Lesser known linux commands: comm

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: