Comm Command in Linux

FREE Online Courses: Click, Learn, Succeed, Start Now!

This article will teach you about the comm command in Linux-based operating systems. We will look at the comm command, why it is used, the syntax of the comm command, and the options used along with it.

At the end, we will also look at some fantastic practical examples of the comm command by pairing it with various options to understand its working. So pay attention, take notes, and read to the end for the best benefits.

What is Linux comm command?

Comm is the abbreviation for “compare”. The comm command is a command-line-based utility in the Linux-based operating system that compares two sorted files line by line and writes to standard output.

For example, if you have two lists of people and need to find out the names available in one, not in the other, or even those common to both, we can use the comm command to achieve this.

The comm command needs two sorted files, which it compares line by line. The comm command compares every line of the files and displays the files’ unique lines and common lines in separate columns.

What is the syntax of Linux comm command?

The syntax of the comm command is very simple. Take a look:

comm <OPTION> <FILE1> <FILE2>

Here are the fields present in the syntax of the comm command.

1. <OPTIONS>

This field takes in a range of options that specify how the comm command must function, format, and print the output. You can also specify more than one option in this field.

2. <FILE1> <FILE2>

These fields take in the filenames of the files you want to compare. For example, if you want to compare two files that are not present in your current working directory, you can enter the complete path of the files.

Options used with comm command in Linux

Unlike most commands in Linux, the comm command comes with very few options. Let us take a detailed look at each.

1. -1

This option suppresses the first column. The first column contains the lines unique to <FILE1>.

2. -2

This option suppresses the second column. The second column contains the lines unique to <FILE2>.

3. -3

This option suppresses the third column. The third column contains the common lines in both <FILE1> and <FILE2>.

4. –help

This option prints the help menu of the comm command, as shown below:

help

5. –check-order

This option checks if the input is correctly sorted, even when all input lines are pairable.

6. –nocheck-order

This option is the opposite of the option “–check-order”. It does not check if the input is correctly sorted.

7. –output-delimiter

This option separates the columns with the string you specified.

8. –version

This option prints the version of the comm command you are using.

version

Understanding output of Linux comm command

Before we look at some fascinating examples of the comm command in action, let us first understand its output. However, before we do so, let us consider the following two files:

file1

file2

You must remember that the file you are comparing using the comm command must always be sorted (alphabetically or numerically). For example, let us compare these two files by using the command shown below:

comm file1 file2

comm output

We can see that the output consists of 3 columns. The first column contains the lines unique to file 1 (not in file2). For example, in our case, the words baking soda, salt, and vanilla extract are unique to file1.

The second column contains the words that are unique to file2. For example, the words chicken salad, onions, pickles, potato chips, soda pop, and tomatoes are unique to file2.

The third column contains common words in both file1 and fil2. In our case, the words all-purpose flour, brown sugar, chocolate chips, eggs, milk, and white sugar are common in both file1 and file2.

Now, since we have covered the theory and fundamentals regarding the comm command, let us look at some fantastic examples of the comm command in the terminal of ubuntu 20.04.

Comparing unsorted files in Linux

Rember when I said that the files comm command compares only sorted files? Well, it is true, but not entirely. Let us first see what happens if we compare two unsorted files (consider the files shown below):

unsorted file first

unsorted file2

Upon comparing the files shown above, we get an error stating that the files are not in sorted order.

comparing unsorted files

We use the option “-no check-order” to eliminate this error. This option does not check the order of the file and still prints out the differences between them.

no check

As you can see, even though the two files are not alphabetically sorted, the comm command still prints the differences between them if you pair it with the “noorder-check command.”

Not printing the lines unique to file1

If you don’t want to print the first column, i.e., the lines unique to the first file, you can pair the comm command with the option “-1”, as shown below:

comm -1 <FILE1> <FILE2>

not printing lines unique to first file

Not printing the lines unique to file2

If you don’t want to print the second column, i.e., the lines unique to the second file, you can combine the comm command with the option “-2”, as shown below:

comm -2 <FILE1> <FILE2>

Not printing lines unique to file2

Not printing the lines common to file1 and file2

If you don’t want to print the third column, i.e., the lines unique to the second file,x, you can combine the comm command with the option “-3”, as shown below:

comm -3 <FILE1> <FILE2>

not printing lines common to first file

Printing only the lines common to file1 and file1

If you want to suppress both column 1 and column 2 and print only the third column (the common lines between file1 and file2), use the syntax below.

comm -12 <FILE1> <FILE2>

files common to first second files

Displaying the number of lines

To know the number of lines in each column, you can execute the comm command along with the “–total” option as shown:

comm -total <FILE1> <FILE2>

displaying the number of lines

Separating the columns

By default, the null value in the table is empty. However, if you want to replace the empty space with a character or string of your choice, use the “–output-delimiter” option followed by the string as shown below:

comm --output-delimiter=<string> <FILE1> <FILE2>

output delimiters

Summary

As you have seen, the comm command compares every line of the files and displays the unique lines and common lines of the files in separate columns.

You have now learned the comm command, why it is used, the syntax, and the options used with it. We have also seen many fascinating examples of the comm command in action.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

Leave a Reply

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