Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I diff 2 files while ignoring leading white space

Tags:

diff

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?

like image 495
Ching Liu Avatar asked May 07 '13 15:05

Ching Liu


People also ask

How do you ignore white space in diff?

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.

Can diff Ignore spaces and blank lines?

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.

How do I ignore whitespace in Linux?

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.

What is white space in files?

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.

How can I ignore whitespace in LINUX files?

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.

How to remove white space between two lines of text in SED?

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.

How do you compare files without comments in Linux?

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.

How does diff work with non-ignorable changes?

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.


2 Answers

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.

like image 135
Lev Levitsky Avatar answered Sep 30 '22 01:09

Lev Levitsky


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) 
like image 34
αғsнιη Avatar answered Sep 30 '22 03:09

αғsнιη