Lloyd Rochester's Geek Blog

Using the comm(1) command in Unix we can see the difference between lists. The comm command takes two files as inputs and will output lines unique in FILE1, lines unique in FILE2 and lines common to FILE1 and FILE2. These difference comparisons are synonymous with taking the union between lists, and left or right joins between them. Note, for the comm command to work the lists need to be sorted.

Example Comparison

Let’s construe an example by creating two lists in a.txt and b.txt and show how the comm command can be used to find the differences in the lists.

Here is an example file named a.txt with contents:

$ cat a.txt
a
b
c
d
e

Another file named b.txt with contents:

$ cat b.txt
a
c
d
e
f
g

Using the comm command we can see the differences between the list in various ways:

$ comm a.txt b.txt
		a
b
		c
		d
		e
	f
	g
$ comm -1 a.txt b.txt
	a
	c
	d
	e
f
g
$ comm -2 a.txt b.txt
	a
b
	c
	d
	e
$ comm -3 a.txt b.txt
b
	f
	g
$ comm -12 a.txt b.txt
a
c
d
e

Note, in the last example above we can mix the 3 options of 123 to get different outputs.