Unix Command to Compare Files — comm
File line comparison with comm — a simple Unix command
At work, I was once tasked to compare two text files and find the common lines.
Normally, I default to writing a simple Ruby script to do these tasks, but recently have discovered ‘comm’ and it ended up being very handy!
comm takes two input files, and compares them line by line.
Usage:
comm left.txt right.txtThe output shows which lines are in common and unique to each file via three columns: Left (1, only in left.txt), middle (2, only in right.txt) or right (3, line present in both files) columns.
For instance, if I had two files representing students enrolled in two classes:
# computer_science.txt
Amy
Bart
Cate
Donald
Fred
# music.txt
Amy
Donald
Ellie
Fred
GeorgeThe result of comm computer_science.txt music.txt is:
% comm computer_science.txt music.txt
Amy
Bart
Cate
Donald
Ellie
Fred
GeorgeSince Amy, Donald and Fred are in both files, they are on the far-right. Bart and Cate are only taking Computer Science, so they are on the left; similarly, Ellie and George are only taking music, so they are on the middle.
Visually, it’s nice to see, but it’s more useful to use comm to filter for specific left/middle/right results.
To get only the common lines in both files, hide columns 1 and 2:
# Intersection of computer_science.txt and music.txt
% comm -12 computer_science.txt music.txt
Amy
Donald
FredIf you want to see what’s unique to the left file, hide columns 2 and 3:
# Unique to computer_science.txt
% comm -23 computer_science.txt music.txt
Bart
CateAnd to see what is unique to the right file, hide columns 1 and 3:
# Unique to music.txt
% comm -13 computer_science.txt music.txt
Ellie
GeorgeAs always, there are many tools for a job, especially for software engineers. My Ruby script works just fine, but comm is so simple to use, I highly recommend it for a quick comparison between two text files!

