The comm utility is powerful, yet not well known.
Named after the word “common”, you can use the comm utility to select or reject lines common to two files.
Practically, once I learned about this tool, I use it often to:
- Get all lines that exist in both files (“select common” lines).
- Find lines that are missing from one file compared to another (“reject common” lines).
Just don’t forget that comm requires that the input files are sorted lexically! That shouldn’t be a problem, since you can always use
sort before calling
comm. But you do need to remember to do this, otherwise you might get unexpected results (which may or may not happen to me on a regular basis).
Shell-Foo credit for this one: Eyal Fink.
Shell-Foo is a series of fun ways to take advantage of the powers of the shell. In the series, I highlight shell one-liners that I found useful or interesting. Most of the entries should work on bash on Linux, OS X and other UNIX-variants. Some probably work with other shells as well. Your mileage may vary.
Feel free to suggest your own Shell-Foo one-liners!
$ seq 9 | grep -v -e 2 -e 6 -e 9 > a $ seq 9 | grep -v -e 2 -e 5 -e 7 > b # Printing only lines that are common to both files: $ comm -12 a b 1 3 4 8 # Printing lines that are unique in a: $ comm -23 a b 5 7 # Printing lines that are unique in b: $ comm -13 a b 6 9
Good to know
As far as I know, the tool is available by default in popular Linux flavors (verified with Debian & Ubuntu). It’s also available on my OS X, though I can’t say for sure whether it was always there, or a result of installing some dev-tools package.