comm command by examples

comm is a useful command for us to compare two sorted files line by line. It can be used in multiple scenarios. Before getting into the use cases, let’s understand how it works first.


How the ‘comm’ command works

When executed without any options

Let’s demonstrate how the comm command works by an example. In this example, we need to create two files:

file1.txt

Apple
Banana
Cat

file2.txt

Banana
Whatever

Let’s see what it would return if we just pass the two files as arguments to the comm command.

noob@learnfromnoobs:~$ comm file1.txt file2.txt 
Apple
                Banana
Cat
        Whatever

The output looks weird… What does it actually want to show us? In fact, the comm command produces a three-column output if there is no options provided.

  • Column 1 – lines unique to FILE1
  • Column 2 – lines unique to FILE2
  • Column 3 – lines that appear in both files

So the output is actually giving us a table like this:

Column 1
lines unique to FILE1
Column 2
lines unique to FILE2
Column 3
lines that appear in both files
Apple
Banana
Cat
Whatever

Suppressing columns

We can use the options -1, -2 and -3 to suppress the corresponding column.

Let’s try suppressing the third column now.

noob@learnfromnoobs:~$ comm -3 file1.txt file2.txt 
Apple
Cat
        Whatever

If you still have trouble understanding it, this updated table maybe help you. The third column is removed as we used the option -3.

Column 1
lines unique to FILE1
Column 2
lines unique to FILE2
Column 3
lines that appear in both files
Apple
Cat
Whatever

Common use cases

The comm command becomes very useful when using different combinations of -1, -2 and -3. Here are a few common use cases.

1. Print lines in file A that do not exist in file B

Let’s say we have two files: file A and file B. We want to remove the lines that appear in file B from file A. We can use comm -23 so it only print out the lines that are unique to file A. All those other lines (lines unique to file B and lines that appear in both files) are removed.

noob@learnfromnoobs:~$ comm -23 file1.txt file2.txt 
Apple
Cat

If you are interested, check out this article for more methods to accomplish this.

2. Print lines that appear in both files

We can make use of comm -12 to print out all the lines that appear in both files.

noob@learnfromnoobs:~$ comm -12 file1.txt file2.txt 
Banana

3. Compare output of two commands

With the help of Process Substitution, we can use comm to compare the output of two commands.

For example, we can compare the ls result of two directories. We can use comm -23 to show the files that are in the directory dir1 but not in the directory dir2.

$ comm -23 <(ls dir1) <(ls dir2)

Conclusion

In this article, we covered the how the comm command works and the common use cases of the comm commands. Now you have just added a new tool to your toolbox. Using comm in the right situation can really help you to save your time and efforts.

I hope you enjoyed this article and learned something new.

Keep learning and have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *