Diff Tool That Ignores Newlines

Viewed 9521

I frequently need to compare SQL procedures to determine what has changed in the newest version. The problem is, everyone has their own style of formatting, and SQL doesn't (usually) care about where one puts their newlines (e.g. where clauses all on one line vs. newline before each AND).

This makes it very difficult (especially for long procedures) to see the actual differences. I cannot seem to find a free diff/merge utility that will allow me to ignore newlines (i.e. treat as whitespace). So far I've tried WinMerge and Beyond Compare without any luck. Does anyone know of a diff tool (ideally free) that would see these two examples as identical?

Ex. 1:

the quick
brown

Ex. 2:

the
quick
brown

Thanks in advance.

12 Answers

You can use the command-line tool wdiff to ignore newlines. wdiff is a GNU tool for comparing files on a word-by-word basis. It can ignore newlines with the -n option.

Suppose I put your 2 example files into ex1.txt and ex2.txt. Then we can run:

$> wdiff -n ex1.txt ex2.txt
the
quick
brown

The output is actually the contents of the first file. Note that there are no + or - signs, which means the files have the same strings.

If I had added "fox" to the end of ex1.txt, then the output would look like this:

the
quick
brown [-fox-]

If seeing the common words still bothers you, you can add -3 or --no-common. Here's the example again where I added "fox" to the first file:

$> wdiff -n -3 /tmp/ex1.txt /tmp/ex2.txt

======================================================================
 [-fox-]
======================================================================
Related