comm

comm – utility for easily comparing files (better than diff)

Posted on Updated on

Comm is a very useful GNU program that is better than diff for finding unique lines between two given files. A good example would be one file with a big list of IP addresses, with another file that has a small list of IP addresses. In this example, the IP addresses in the small list are presumed to be present in the big list, but short of doing an advanced script using find, this is a quick and clean way to find the duplicates.

With no options, produce three-column output. Column one contains lines unique to file1, column two contains lines unique to file2, and column three contains lines common to both files.

-1 suppress lines unique to file1
-2 suppress lines unique to file2
-3 suppress lines that appear in both files

NOTE: The files MUST be sorted first, or the results will not be accurate.

##################################

contents of file1:
192.168.1.0
192.168.1.1
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
201.44.32.4
201.44.32.5
201.44.32.8
201.44.32.9

contents of file2:
192.168.1.1
192.168.1.5
201.44.32.5
201.44.32.8

#################################

Example (to find only the unique IP address in file1)

comm -3 file1 file2

# output
192.168.1.0
192.168.1.4
192.168.1.6
192.168.1.7
201.44.32.4
201.44.32.9

Advertisements