compare files

August 23, 2021



Only recently I found comm to compare two files. * Don't miss to read man comm.

comm expects sorted input.

comm accepts the parameters (among others) -1, -2, -3 or a combination of them. They refer to output columns - When provided the according column is left out:

$ man comm
-1     suppress column 1 (lines unique to FILE1)
-2     suppress column 2 (lines unique to FILE2)
-3     suppress column 3 (lines that appear in both files)

Imagine you had file1:

bamboo
bass wood
mobile phone
plate
paper
towel

and file2:

bass wood
mobile phone
cup
paper
handkerchief

Using comm you can now show what can be found:

$ comm -23 <(sort -u file1) <(sort -u file2)
# Output
bamboo
plate
towel
$ comm -13 <(sort -u file1) <(sort -u file2)
# Output
cup
handkerchief
$ comm -12 <(sort -u file1) <(sort -u file2)
# Output
bass wood
mobile phone
paper