You are not logged in.

#1 2009-07-06 12:49:50

Vortex375
Member
Registered: 2009-02-24
Posts: 61

diff to only show changes in the amount of white space

Hi,

the diff tool has this handy option:

-b  --ignore-space-change
              Ignore changes in the amount of white space.

However, what I need would be the exact opposite: I want diff to only show lines where the amount of whitespace changed and not show lines where anything else (i. e. the text) has changed.

Since I couldn't find an option that does this on the man page, I thought it might be possible to do this via a Regular Expression. But I don't know how to write those, especially not for this specific case.

Another way would be to create two diffs: One that shows all changes and one that ignores whitespace changes. Then it would theoretically be possible to filter out those lines where only the amount of whitespace changed, because they would only show up in one of the two diffs. But I don't know how to filter them out. neutral

Can anybody help me with this, please? Maybe there is an even easier way to do this?

Offline

#2 2009-07-06 13:14:59

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: diff to only show changes in the amount of white space

Something like this?

--> sedcmd='s/[^[:blank:]]//g;s/ /(space)/g;s/\t/(tab)/g'
--> diff <(sed "$sedcmd" test1 | cat -n) <(sed "$sedcmd" test2 | cat -n)
1,3c1,3
<      1    
<      2    (space)
<      3    (space)(tab)(space)(tab)
---
>      1    (space)(tab)
>      2    (space)(space)
>      3    (space)(space)(tab)(space)(tab)

Offline

#3 2009-07-06 15:52:16

Vortex375
Member
Registered: 2009-02-24
Posts: 61

Re: diff to only show changes in the amount of white space

That's not quite what I meant. I don't want to make whitespace changes visible. I want diff to ignore all changes except for whitespace change.

I'll try to make it clear with an example:

I want to compare two files, incidentally named "file1" and "file2". The files' contents are as follows:

File1:

foo bar
baz

File2:

foo     bar
siegfried

As you can see, line 1 only differs in the amount of whitespace, whereas line 2 is completely different.

If I run "diff file1 file2" it shows me 2 changed lines:

$ diff file1 file2
1,2c1,2
< foo bar
< baz
---
> foo     bar
> siegfried

If I run diff with the "-b" Option to ignore whitespace changes it only shows me the change in line 2, of course:

$ diff -b file1 file2
2c2
< baz
---
> siegfried

However, I want it to only show me the change in line 1 instead.
Show lines where only the amount of whitespace has changed.
Don't show lines where anything else has changed.

Thanks for your help!

Last edited by Vortex375 (2009-07-06 15:55:52)

Offline

#4 2009-07-06 18:22:05

Ranguvar
Member
Registered: 2008-08-12
Posts: 2,549

Re: diff to only show changes in the amount of white space

Do a normal diff and a diff -b, and take the diff of those two? Well, something along those lines (probably need to strip some junk)? big_smile

Offline

#5 2009-07-06 19:57:31

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: diff to only show changes in the amount of white space

Ah I get it now. I wrote a script below, it is a reverse of what I was thinking in 2, but it doesn't use diff so it may not be usable to you (such as writing a patch). I'm not sure how to change the output of diff, but if you can get it print the line number on every line then you can use sort and uniq -d on that too.

But try this:

#! /bin/bash
FILEA=$1
FILEB=$2

sedcmd='s/[[:blank:]]//g'
chline=$(sort -k1,1 <(sed "$sedcmd" "$FILEA" | cat -n) <(sed "$sedcmd" "$FILEB" | cat -n) | uniq -d | cut -f 1 | uniq | sed 's/[^0-9]*\([0-9]\)*.*/:\1:/')
grep -n '' "$FILEA" "$FILEB" | grep "$chline" | sort -n -t: -k2,2 | sed 's/:/\t/'

edit: sort needed -n

Last edited by Procyon (2009-07-07 07:52:04)

Offline

Board footer

Powered by FluxBB