8

Tip #186   Using comm

You can use diff to see the differences between two files, but it can be useful to see what is the same and more clearly how they differ. This is where comm comes in useful.

comm tells you what information is common to two lists and what information appears uniquely in one or the other.

$ find . -type f -print -exec cat {} \;
./1.txt
a
b
c
./2.txt
a
c
e

$ comm 1.txt 2.txt 
                a
b
                c
        e


The first column shows lines only in the first file, the second column lines from the second file and the third column lines from both.

This can be made easier still by adding a bit of perl:
$ comm 1.txt 2.txt | perl -pe 's/^/1: /g;s/1: \t/2: /g;s/2: \t/A: /g;' | sort
1: b
2: e
A: a
A: c