Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to diff two files, such that end of lines and white space be ignored. Namely, I would like diff to find no difference between d1.txt and d2.txt:

$ cat d1.txt                                                                    
test1                                                                           

test2                                                                           

test3                                                                           

 test4                                                                          
$ cat d2.txt                                                                    
test1test2test3test4               

For some reason,

diff -d -w -a --strip-trailing-cr d1.txt d2.txt

does not do the job. Any help is appreciated.

share|improve this question
1  
What OS are you on? There are differences between diff implementations. Is this Linux (which one)? Unix? OSX? Irix? –  terdon Sep 2 '13 at 16:03
    
I am on Fedora linux. I thought that GNU diff was used everywhere. My version is $ diff -v diff (GNU diffutils) 3.3 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Paul Eggert, Mike Haertel, David Hayes, Richard Stallman, and Len Tower. –  ffcitatos Sep 3 '13 at 8:49
    
GNU diff is used on GNU systems, Linux machines. You could have been using any OS. –  terdon Sep 3 '13 at 11:50
add comment

1 Answer

up vote 0 down vote accepted

diff compares lines, see man diff:

diff - compare files line by line

Ignoring white space means that foo bar will match foobar if on the same line. Since your patterns in d1.txt span multiple lines, the files will always differ. I haven;t actually read the source code but I guess diff works something like:

for each line number X in file1;
    line1 = line X from file1
    line2= line X from file2
    If line1 is equal to line2 the do something
    else do something else

The first line of your file1 is not the same as the first line of file2 so a difference is reported. If you really want to check that the files contain the exact same non-whitespace characters, you could try something like this:

diff <(perl -ne 's/\s*//xg; print' d1.txt) <(perl -ne 's/\s*/g; print' d2.txt)
share|improve this answer
    
Thanks for the reply, but that still is not it. Perhaps I am using a wrong tool then? Is there no piece of software that does what I want? –  ffcitatos Sep 3 '13 at 8:51
    
@ffcitatos what do you mean that's not it? What did it do? –  terdon Sep 3 '13 at 11:52
    
Thanks, terdon, I have accepted your answer. My question is not formulated well enough, but I cannot find a way to unambiguously state what I want ;-) –  ffcitatos Sep 4 '13 at 8:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.