Lesser known linux commands: comm

November 29, 2009

comm compares two sorted files and produces three column output. First column contains lines only in first file,second column contains lines only in second file and third column contains common line.

Let me explain with an example.

$ cat f1

line 1

line 2

line 3

line 4

line 5

$ cat f2

line 1

line 2

line 2a

line 3a

line 4

line 5

$ comm f1 f2

line 1

line 2

line 2a

line 3

line 3a

line 4

line 5

comm has options like

-1 suppress first column -2 suppress second column -3 suppress 3rd column.

You can combine them also. e.g if you want only common lines use -12.

Where is it useful? Once, two persons have modified spreadsheet copied out of same source. I wanted to extract unique rows from each and insert into original. It is tedious to compare the changes visually. I dumped them into csv, sorted and ran comm to examine and merge with originial.

Advertisements