How can I diff 2 files while ignoring leading white space

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 one is using tabs incorrectly, you can fix that

expand bad_file

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.

Tags:

Diff