Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

class foo { 

is equally equivilant to

class foo{bar spaz}

or, as well

classfoo { 

but NOT

classfoo { 

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.
share|improve this question
By design, diff compares files line by line. The newlines serve as delimiters for the changes. Without them it would be hard to do a meaningful comparison. shurane's answer would work, but it is less than useful for the reason set out in the comments. – Roland Smith Oct 5 '13 at 15:55
How the heck in any programming language is "barspaz" the same as "bar spaz"? Very strange. – Peon Oct 5 '13 at 16:41
You could replace all whitespace with linebreaks, then diff the results. That would generate lots of lines of output for large chunks of differences, though. I think you'd need a new program (or a diff flag) to compare files without treating linebreaks specially. – Blacklight Shining Oct 6 '13 at 16:41
This question has a possible duplicate. – Blacklight Shining Oct 6 '13 at 16:45
Try wdiff too. It's not line-based like diff, so it manages your first equivalence (though not your second). – ShreevatsaR Dec 1 '14 at 19:57
up vote 2 down vote accepted

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 Let me show you how tr processes it:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
C:\temp>tr -d '[:space:]' <
share|improve this answer
The problem with this is it gives you a single line of everything mashed together. If you tried to feed that into diff, diff would tell you that each file has one line, and the lines are different. Not very helpful. – Blacklight Shining Oct 5 '13 at 15:39
@BlacklightShining Well, yes, but that is what the question asked for... – evilsoup Oct 5 '13 at 15:45
@evilsoup In that case, I have a more efficient solution. It comes preinstalled in virtually every system and can be found at /bin/false. – Blacklight Shining Oct 5 '13 at 17:46
@BlacklightShining Presumably the OP will be working with very short files where there is a chance of them matching up after the whitespace is removed. I can't think of a specific task where this would be useful... but then that's the point of having these modular *nix commands: so that they can be used together for niche tasks that most people would never think of. – evilsoup Oct 5 '13 at 18:00
Essentially I am trying to compare two pieces of CSS, one has been partially minified (e.g. some rules are in one line), the other has not. They contain about 90% of the same rules. I think I'm just going to write a custom parser. – A.B. Carroll Oct 9 '13 at 17:58

Despite being line oriented, diff could be made to do what's being requested. Roughly, you could do the diff -w and then post process the diff output. It could look at the diff output and join various pairs of lines to see if they (now) match the line in the other file.

Seems like it might be O(n^2) or other nasty thing, but it would still be very helpful if it limited it self to (say) no join, join 2 lines, join 3 lines on file 1 X no join, join 2 lines, join 3 lines on file 2 on a moving "window" of the diff output. (where X is ~ "cross product")

In fact, seems like a job that a Perl diff output post-processing script could do with few hours of work (depending on your (Perl) programming skill)

Maybe when I have some task that I don't really want to be doing at work ... :-)

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .