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!