Question: I am currently working on a source code generation tool. To make sure that my changes do no introduce any new bugs, a `diff` between the output of the program before and after my changes would theoretically be a valuable tool.

However, this turns out to be harder than one might think, because the tool outputs lines where the order does not matter (like `import` statements, function declarations, …) in a semi-randomly ordered way. Because of this, the output of `diff` is cluttered with many changes that are in fact only lines moved to another position in the same file.

Is there a way to make diff ignore these moves and only output the lines that have really been added or removed?

Answer: 
You may try to sort'em first. Something like:

```
sort file-a > s-file-a
sort file-b > s-file-b
diff s-file-a s-file-b

```

Bash (and zsh) can do this in one line with [process substitution](http://tldp.org/LDP/abs/html/process-sub.html)

```
diff <(sort file-a) <(sort file-b)

```
If the file is structured into sections, it is just the sections that are out of order, and there exists a regular expression that you can use to recognize the section header, you could [csplit](https://en.wikipedia.org/wiki/Csplit) the files into their sections and then compare the sections pairwise.

For instance, I just did this on two MySQL dumps to compare them after some of the database names had changed case (and therefore the dump listed them in a different order):

```
csplit all-07sep2015-11:19:12.sql '/Current Database/-1' '{*}'  # split the dump made before the change, creating files xx00, xx01, ...
csplit -f yy all-07sep2015-12:26:12.sql '/Current Database/-1' '{*}' # split the dump made after the change, creating files yy00, yy01, ...
fgrep 'Current Database' xx?? yy?? | perl -lne 'BEGIN{my %foo}; /(^....).*`(.*)`/ and push(@{$foo{lc($2)}}, $1); END {printf("diff -di %s %s\n", @{$_}) for values %foo}' | sh -x | less  # match the pairs and compare them with diff

```