I have 2 source files, they are different versions of the same thing. However, one has been through a different editor that made indent changes, so all the lines are showing up different in diff. Is there a diff command or a filter I can use to diff with so that the output will only be lines that are different after ignoring the leading spaces/tabs?
asked May 7, 2013 at 15:33 1,616 2 2 gold badges 12 12 silver badges 18 18 bronze badgesdiff has some options that can be useful to you:
-E, --ignore-tab-expansion ignore changes due to tab expansion -Z, --ignore-trailing-space ignore white space at line end -b, --ignore-space-change ignore changes in the amount of white space -w, --ignore-all-space ignore all white space -B, --ignore-blank-lines ignore changes whose lines are all blank
So diff -w old new should ignore all spaces and thus report only substantially different lines.
answered May 7, 2013 at 15:37 Lev Levitsky Lev Levitsky 65.3k 23 23 gold badges 151 151 silver badges 181 181 bronze badgesIt's worth noting that -w effectively removes all whitespace from the lines before comparing, so ab and a b are considered identical. I prefer -b because it ignores whitespace changes, meaning that ab and a b are considered different but a b and a + multiple spaces + b (sorry, mini-Markdown wouldn't allow multiple spaces in code!) are considered the same.
Commented Oct 16, 2015 at 7:56 How can I ignore all the newlines? Commented Sep 29, 2016 at 23:15 Can I tell diff to ignore equivalent series expansions for transcendental numbers like e and pi? Commented Mar 27, 2017 at 20:13@RichardWiseman Good point; in cases when there is a mess of tab and space differences though, there's no good combination that works except -w . -tb , -tbB , -t , etc. all leak in some differences that you'd prefer to ignore. -w does not, even if it also might exclude some differences you'd prefer to see.
Commented Apr 27, 2018 at 17:29@geneorama Yes, and you can ignore any change that does not alter the meaning of your program with the new --ignore-goedel option.
Commented Oct 10, 2022 at 8:26diff -bB file[12]
-b, --ignore-space-change ignore changes in the amount of white space -B, --ignore-blank-lines ignore changes whose lines are all blank
Please note that -w option will ignoring all whitespaces before diffing, so a line like this i s a line and this is a line in each file will compare as thisisaline and will not report differences.
Beside of -w option problem, even -b option has minor issues and that doesn't ignore whitespaces if come at the begging of a line
So you should use sed to remove those whitespaces occurred at start first then do `diff -bB.
diff -bB <(sed 's/^[ \t]*//' file1) <(sed 's/^[ \t]*//' file2)
answered Sep 28, 2017 at 16:38
2,733 2 2 gold badges 28 28 silver badges 41 41 bronze badges
If one is using tabs incorrectly, you can fix that
expand bad_file
answered May 7, 2013 at 15:38
My open-source Linux tool 'dif' compares files while ignoring various differences including whitespace.
It has many other options for ignoring comments or timestamps, sorting the input files, doing search/replace, ignoring certain lines, etc.
After preprocessing the input files, it runs the Linux tools meld, gvimdiff, tkdiff, or kompare on these intermediate files.
Installation is not required, just download and run the 'dif' executable from https://github.com/koknat/dif
To condense any whitespace to a single space, use the -white option:
dif file1 file2 -white
To remove all whitespace (except for newlines), use the -nowhite option:
dif file1 file2 -nowhite