0

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.

3
  • 1
    What OS are you on? There are differences between diff implementations. Is this Linux (which one)? Unix? OSX? Irix?
    – terdon
    Sep 2, 2013 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, 2013 at 8:49
  • GNU diff is used on GNU systems, Linux machines. You could have been using any OS.
    – terdon
    Sep 3, 2013 at 11:50

2 Answers 2

0

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)
3
  • 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, 2013 at 8:51
  • @ffcitatos what do you mean that's not it? What did it do?
    – terdon
    Sep 3, 2013 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, 2013 at 8:09
0

Here's a shell script that I think will do what you want. It's not as cool as @terdon's oneliner, but it does show characters that differ.

#!/bin/bash
f1=`mktemp`
f2=`mktemp`
result=`mktemp`

# Do the comparison letter-by-letter
cat "$1" | tr -d '[:space:]' > "$f1"
cat "$2" | tr -d '[:space:]' > "$f2"
cmp -l "$f1" "$f2" > "$result"
retval=$?

# Print the results as characters rather than octal values
if [ "$retval" -ne 0 ]; then
  while read -a vals; do
    echo -e "${vals[0]}" '\0'"${vals[1]}" '\0'"${vals[2]}"
  done < "$result"
fi

rm -f "$f1" "$f2" "$result"
exit $retval

Thanks to https://stackoverflow.com/a/14267178/2877364 and https://stackoverflow.com/a/4642213/2877364 !

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .