Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diff Tool That Ignores Newlines [closed]

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.

like image 221
Prometheus Avatar asked Nov 20 '09 21:11

Prometheus


9 Answers

I really like SourceGear's DiffMerge!

It works on all platforms and has built in rulesets, but allows you to create and add your own. Which means that you can ignore what you want, when you want it.

Bonus, it is free!

like image 106
Dustin Laine Avatar answered Oct 03 '22 20:10

Dustin Laine


What i've done in my own similar case is to use a sql prettifier which will organize two sets of semi-disparate SQL in very similar fashion automatically. i then paste and compare the results with WinMerge.

It's a two-step process but it's much more palatable than many other options, especially when many lines of code are involved.

Link to web-based Sql Pretty printer that's decent.

like image 30
Paul Sasik Avatar answered Oct 03 '22 22:10

Paul Sasik


Compare++ is an option, you can try "Ignore code style changes" in the 'smart' menu. It support structured comparison for many langugages such as C/C++, JavaScript, C#, Java, ...

like image 22
winlts Avatar answered Oct 03 '22 22:10

winlts


I love Araxis merge. Not free but well worth it. it can, among other things, ignore any kind of whitespace if you want.

like image 24
Byron Whitlock Avatar answered Oct 03 '22 22:10

Byron Whitlock


You can use The DTP (Data Tool Project) of the Eclipse IDE.

To show it I created two almost identical SQL files and let eclipse show me the differences. After clicking "show next" I took a screenshot.

As you can see it still highlights the newlines, but by the way it does you can immediately see that they contain no substantial change to the SQL. It's easy to spot where I changed the ID from 1 to 2.

Here's the result.

alt textalt textalt text

like image 36
lothar Avatar answered Oct 03 '22 21:10

lothar


Regardless on your definition of "Free" (beer vs speech/libre), Poor Man's T-SQL Formatter is also available to do this, either with WinMerge (using the winmerge plugin) or Beyond Compare and other comparison tools that allow for command-line pre-formatting, using the command-line bulk formatter.

If you prefer to take it for a whirl without downloading anything, it's available for immediate use online (like its non-libre counterparts T-SQL Tidy, Instant SQL Formatter, etc):

http://poorsql.com

like image 33
Tao Avatar answered Oct 03 '22 20:10

Tao


Our SD Smart Differencer compares two source programs according to their precise grammatical syntax and structure, rather than according to raw text. It does so by parsing (SQL) source the way a compiler would, and comparing the corresponding compiler data structures (e.g., abstract syntax trees). The SmartDifference consequently does not care about newlines, whitespace or intervening comments.

It reports differences, not in terms of line breaks, but rather in terms of programming language structures (variables, expressions, statements, blocks, functions, ...) and in terms close to programmer intentions (delete, insert, move, copy, rename) rather than line-insert or line delete.

SQL (like many other computer language names) is the name of a family of computer languages that are similar in syntax but differ in detail. So for the Smart Differencer, which dialect of SQL you are using matters. We have SQL front ends (therefore SmartDifferncers) for PLSQL and SQL2011. To the extent you SQL stays within the bounds of either of these, the Smart Differencer can work for you; to the extent you use extra goodies of SQL Server or Postgres, the SmartDifferencer presently can't help you. [We develop language parsers as part of our business, so I expect this is a matter of delay rather than never].

While the OP asked about SQL in the details, his headline question is language agnostic. There are SmartDifferencers already for many other widely used languages other than SQL too: C, C++, C#, Java, ...

like image 28
Ira Baxter Avatar answered Oct 03 '22 21:10

Ira Baxter


Another alternative is Emacs' Ediff. Works great if you are not afraid of Emacs.

like image 34
MarcH Avatar answered Oct 03 '22 22:10

MarcH


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-]
======================================================================
like image 43
nofinator Avatar answered Oct 03 '22 20:10

nofinator