ANALYTICS. VISUALS.

AND SOME OTHER RANDOM SELF NOTES.

May 28, 2008

How to use diff in UNIX

The following were taken from http://www.unixtutorial.org/2008/02/compare-text-files-using-diff/.

All credits goes to them.

If you need to compare two text files in Unix, you're mostly likely to use the diff command.

Today I'll talk about the simplest scenario: you want to compare two files and understand if there are any differences.

Suppose you have two files in /tmp directory:
/tmp/1.txt:

aaa
bbb
ccc
ddd
eee
fff
ggg

and /tmp/2.txt:

bbb
c c
ddd
eee
fff
ggg
hhh

I have deliberately created them so short and simple - this way it's easier to explain how the comparison works. If there are no differences between the files, you will see no output, but if two text files are indeed different, all the text mismatches will be highlighted using the standard diff output:

$ diff /tmp/1.txt /tmp/2.txt
1d0
<> c c
7a7
> hhh

Lines like "1d0" and "3c2" are the coordinates and types of the differences between the two compared files, while lines like "<>" and "> hhh" are the differences themselves.

Diff change notation includes 2 numbers and a character between them. Characters tell you what kind of change was discovered:

d - a line was deleted
c - a line was changed
a - a line was added

Number to the left of the character gives you the line number in the original (first) file, and the number to the right of the character tells you the line number in the second file used in comparison.

So, looking at the two text files and the diff output above, you can see what happened:

This means that 1 line was deleted. <>
1d0
<>

And this means that the line number 3 has changed. You can see how this confirms that in the first file the line was "ccc", and in the second it now is "c c".

3c2
<> c c

Finally, this confirms that one new line appeared in the second file, it's "hhh" in the line number 7:

7a7
> hhh

That's all you need to know to start playing with text comparisons yourself.

And that's how you do it. It's nice, but not as useful if you have like a thousand line of difference to look at. At times I'd prefer to use the plugin "Compare" in Notepad++. It's easier on the eyes.

Post a Comment