Comm Command
Comm Command
• About comm
• comm syntax
• comm examples
• Related commands
• Linux and Unix commands help
About comm
Compare two sorted files line-by-line.
Description
Compare sorted files FILE1 and FILE2 line-by-line.
With no options, comm produces 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.
Each of these columns can be suppressed individually with options.
comm syntax
comm [OPTION]... FILE1 FILE2
Options
-1 suppress column 1 (lines unique to FILE1)
--check-order check that the input is correctly sorted, even if all input lines are pairable
Examples
Let's say you have two text files, recipe.txt and shopping-list.txt.
recipe.txt contains these lines:
All-Purpose Flour
Baking Soda
Bread
Brown Sugar
Chocolate Chips
Eggs
Milk
Salt
Vanilla Extract
White Sugar
All-Purpose Flour
Baking Soda
Bread
Brown Sugar
Chicken Salad
Chocolate Chips
Eggs
Milk
Onions
Pickles
Potato Chips
Salt
Soda Pop
Tomatoes
Vanilla Extract
White Sugar
Here, each line of output has either zero, one, or two tabs at the beginning, separating the output into
three columns:
1. The first column (zero tabs) is lines that only appear in the first file.
2. The second column (one tab) is lines that only appear in the second file.
3. The third column (two tabs) is lines that appear in both files.
(The columns overlap visually because in this case, our terminal prints a tab as eight spaces. It might
look different on your screen.)
Next, let's look at how we can bring our separated data into a spreadsheet.
This time there is no output on the screen. Instead, output is sent to a file called output.csv. To check
that it worked correctly, we can cat the contents of output.csv:
cat output.csv
All-Purpose Flour
Baking Soda
Bread
Brown Sugar
Chicken Salad
Chocolate Chips
Eggs
Milk
Onions
Pickles
Potato Chips
Salt
Soda Pop
Tomatoes
Vanilla Extract
White Sugar
Suppressing columns
If you only want to output specific columns, you can specify the column numbers to suppress in the
command, preceded by a dash. For instance, this command will suppress columns 1 and 2, displaying
only column 3 — lines shared by both files. This isolates the items on the shopping list that are also
part of the recipe:
comm -12 recipe.txt shopping-list.txt
All-Purpose Flour
Bread
Brown Sugar
Chocolate Chips
Eggs
Milk
White Sugar
The next command will suppress columns 2 and 3, displaying only column 1 — lines in the recipe that
are not in the shopping list. This shows us what ingredients we already have in our cupboard:
comm -23 recipe.txt shopping-list.txt
Baking Soda
Salt
Vanilla Extract
And the next command will suppress column 3, displaying only columns 1 and 2 — the items in the
recipe that are not on the shopping list, and the items on the shopping list that are not in the recipe, each
in their own column.
comm -3 recipe.txt shopping-list.txt
Baking Soda
Chicken Salad
Onions
Pickles
Potato Chips
Salt
Soda Pop
Tomatoes
Vanilla Extract