Linux – Diff to actually totally ignore whitespace

diff()linuxwindows

I am looking for an application that will diff two files, and actually ignore all whitespace, so for example:

class foo { 
  bar
  spaz 
}

is equally equivilant to

class foo{bar spaz}

or, as well

classfoo { 
  barspaz}

but NOT

classfoo { 
  spazspaz
}

i.e. it would show me that spaz in the previous example has taken the place of bar in any of the other examples. It only needs to compare 2 files.

  • It can be a windows or linux/unix/posix-compatible utility
  • I've tried the lin/unix diff -w command, it only ignores whitespace if the difference per-line is whitespace. I don't see an option to totally ignore whitespace.
  • I also tried UECompare or Ultracompare, a non-free comparison utility for windows.

Best Answer

Are you looking for something like the tr command? Here are the manpages. It's included with msysgit, cygwin, and gnuwin32 tools as far as I can tell.

So you can remove all the whitespace prior to diffing by doing something like:

tr --delete '[:space:]' <filename.txt

You can then feed the output of that command to diff and have it work without having any whitespace.

For example, I have a file named HelloWorldApp.java. Let me show you how tr processes it:

C:\temp>cat HelloWorldApp.java
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}
C:\temp>tr -d '[:space:]' <HelloWorldApp.java
classHelloWorldApp{publicstaticvoidmain(String[]args){System.out.println("HelloWorld!");//Displaythestring.}}