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?
The --ignore-trailing-space ( -Z ) option ignores white space at line end. For many other programs newline is also a white space character, but diff is a line-oriented program and a newline character always ends a line.
So diff -w old new should ignore all spaces and thus report only substantially different lines. It's worth noting that -w effectively removes all whitespace from the lines before comparing, so ab and a b are considered identical.
The --ignore-trailing-space ( -Z ) option ignores white space at line end. Re: "-w or --ignore-all-space option does not ignore newline-related changes" So -w ignores all whitespace, except for the whitespace it doesn't ignore.
Alternatively referred to as spacing or whitespace, white space is any section of a document that is unused or space around an object. White spaces help separate paragraphs of text, graphics, and other portions of a document, and helps a document look less crowded.
It has many other options for ignoring whitespace 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.
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. Show activity on this post. Show activity on this post.
My open-source Linux tool 'dif' compares files while ignoring various differences such as comments. It has many other options for ignoring whitespace or timestamps, sorting the input files, doing search/replace, ignoring certain lines, etc.
In other words, for each non-ignorable change, diff prints the complete set of changes in its vicinity, including the ignorable ones. You can specify more than one regular expression for lines to ignore by using more than one -I option. diff tries to match each line against each regular expression, starting with the last one given.
diff
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.
diff -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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With