Updated April 18, 2023
Introduction to Linux comm
The Linux comm command is using to compare the two sorted files line by line and those lines are common and the lines are unique will write to standard output. The comm command providing the comparison facility by column level. The Linux comm command is written by Richard M. Stallman and David MacKenzie.
Syntax of comm command
comm [ OPTION ] … [ FILE 1 ] [ File 2 ]
1. comm: Using the comm keyword in the command | syntax. We need to provide at list two files to comm command as an argument.
2. OPTION: As per the requirement, we can provide the valid options in comm command. If we will not use any option in comm command, then the comm command produces the three different column output
- The first column output holds the lines matches to FILE1
- The second column output holds the lines matches to FILE2
- The third or last output column hold those lines are common or similar to both the files
3. If we are providing the two sorted input files as an argument to comm command then only the command will be executed properly without and errors.
How does Linux comm Command work?
The comm command reads the two files as an input but it is providing the single output file. The output file will contain three different output columns. The first column output will display the unique lines of the first input file. Similarly, the second column output will display the unique lines of the second input file and the last or third column will display the common data concerning both the files.
Generally, the columns are separated by “Tab”. If the input file or the input data containing the separator character in it then the output columns will become ambiguous.
Examples of Linux comm
Here are the following examples mentioned:
Comm Command
The comm command is the basic command. Now we will look at the default working of it.
Command:
cat f1.txt
cat f2.txt
comm f1.txt f2.txt
Explanation :
We are having a sample directory. In the same directory, we have two files “f1.txt” file and “f2.txt” file (refer screenshot 1 (a) & (b)). In the “f1.txt” file, we are having some data in it and the same format data is present in the “f2.txt” file. While using comm command on “f1.txt” & “f2.txt”, the first column will display the uncommon data of “f1.txt” file, the second column will display the uncommon data of “f2.txt” file and the third column will display the common data of “f1.txt” file and “f2.txt” file (refer screenshot 1 (c)).
Output :
Screenshot 1 (a)
Screenshot 1 (b)
Screenshot 1 (c)
Example #1 – Comm Command with -1
In the comm command, we are having the functionality to skip the column output data. To skip the first column output data, we can use the “-1” option with comm command.
Command :
comm -1 f1.txt f2.txt
Explanation :
In the above command, we are comparing two files like f1.txt and f2.txt file. Using the comm command, we will get the three output columns. While using the “-1” option, we can skip the 1st column output and display the reaming 2 columns output.
Output :
Example #2 – Comm Command with -2
In comm command, we can skip the second column output data using the “-2” option with the command.
Command :
comm -2 f1.txt f2.txt
Explanation :
In the above command, we are comparing two files like f1.txt and f2.txt file. Using the comm command, we will get the three output columns. While using the “-2” option, we can skip the 2nd column output and display the reaming 2 columns output.
Output :
Example #3 – Comm Command with -3
In comm command, we can skip the third column output data using the “-3” option with the command.
Command :
comm -3 f1.txt f2.txt
Explanation :
In the above command, we are comparing two files like f1.txt and f2.txt file. Using the comm command, we will get the three output columns. While using the “-3” option, we can skip the 3rdcolumn output and display the reaming 2 columns output.
Output :
Example #4 – Check Order Option
The check order option is useful to check whether the input file data is correctly sorted order or not.
If the input data is not in sorted order, then the check order option will give an error in comm command.
Command:
comm --check-order f1.txt f2.txt
Explanation :
We are using the check order option in comm command. If the input data is in the sorted format then it will not through an error and if not then it will through an error.
Output :
Example #5 – Nocheck Order Option
The nocheck order option is not checking whether the input file is in a proper sorted format or not.
Command :
comm --nocheck-order f1.txt f2.txt
od -N5 -c od.txt
Explanation :
We are using the nocheck order option in comm command. The nocheck option is not checking the input data is in sorted format or not. It will use when we need to skip the sorting check.
Output :
Example #6 – Output Delimiter Option
In comm command, by default, we will get the output in three different columns. However, if we want to change the output as per the separator we can do it via “output delimiter option”. This option is required for specifying the string that we want to use as the separator.
Command :
comm --output-delimiter=Test f1.txt f2.txt
Explanation :
We have seen earlier, in comm command we are getting the output data in three different columns. With the help of the “output-delimiter” option, we can replace the spaces with own input string. In the above command, we have replaced the space with the string “Test”.
Output :
Conclusion
We have seen the uncut concept of “Linux comm Command” with the proper example, explanation, and command with different outputs. The comm command is useful to compare the two input files whether the input file is in sorted format or not (depend on the condition). In the comm concept, we are satisfying the multiple comparing conditions in it.
Recommended Articles
We hope that this EDUCBA information on “Linux comm” was beneficial to you. You can view EDUCBA’s recommended articles for more information.