Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use diff to see whitespace changes?

I found this question which has answers for git diff. However, I am not comparing files using any sort of version control (I don't even have one available on the machine I am trying to compare from).

Basically, similar to the referenced question, I am trying to see the changes in whitespace. The diff command might show:

bash-3.2$ diff 6241 6242
690c690   
<         
---       
>         

But I don't know if that is a newline, a newline and space, or what. I need to know the exact changes between two documents, including whitespace. I have tried cmp -l -b and it works, but it is rather difficult to read when there are a lot of changes to the point where it isn't really useful either.

What I really want is some way for whitespace to be rendered in some way so I can tell exactly what the whitespace is, e.g. color or perhaps ^J, ^M, etc. I don't see anything in the manual; diff --version shows GNU version 2.8.1.

As a further example, I have also tried piping the output of diff through hexdump.

bash-3.2$ diff 6241 6242 | hexdump -C                                         
00000000  36 39 30 63 36 39 30 0a  3c 20 0a 2d 2d 2d 0a 3e  |690c690.< .---.>|
00000010  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000020  20 20 20 20 0a                                    |    .|           

From this it is obvious to me that a bunch of space characters were added. However, what is not obvious is that a space was inserted before the newline, which is what cmp tells me:

bash-3.2$ cmp -l -b 6241 6242
33571  12 ^J    40           
33590  40       12 ^J        
33591 165 u     40           
...
like image 749
Michael Avatar asked Apr 26 '14 16:04

Michael


1 Answers

There is no easy way to do this with the diff commmand alone. One way to solve your problem is to use cat -te which will turn tab characters into ^I and will write $ at the end of lines, making it easier to see.

$ printf >test1 'hello \t \n'
$ printf >test2 'hello  \t\n'
$ diff test[12] | cat -te
1c1$
< hello ^I $
---$
> hello  ^I$
like image 173
Stuart Caie Avatar answered Dec 08 '22 12:12

Stuart Caie