To compare two sorted files, we use the comm command in the Linux system.
The comm command is used to compare two sorted files line by line and writes three columns to standard output. The first two columns contain lines unique to the first and the second file and the last column contains lines common to both. Columns are distinguished with the tab. The functionality of the comm command is similar to the diff command.
The general syntax of the comm command is as follows −
comm [OPTION]... FILE1 FILE2
Brief description of options available in the comm command.
|Sr.No.||Option & Description|
Suppress first column (lines unique to FILE1)
Suppress second column (lines unique to FILE2)
Suppress third column (lines common in FILE1 and FILE2)
Check that the input is correctly sorted or not, even if all input lines are pairable
Don’t check that the input is correctly sorted
Separate columns with given string STR
Display a summary
Line delimiter is NULL, not newline
Displays a help message and then exits.
It gives info about the version and then exits.
Here, we will compare two sorted file and using the comm command without any option and display all contents in a column in standard output.
vikash@tutorialspoint: ~/Documents$ comm file_first file_second Abhishek Anand Annie Bidu Bruce Celesy Chiku Sayani Vikash Wandra vikash@tutorialspoint: ~/Documents$
Here, we will compare two sorted file and suppress first column using -1 option with the comm command.
vikash@tutorialspoint: ~/Documents$ comm -1 file_first file_second Annie Bidu Chiku Sayani Wandra vikash@tutorialspoint: ~/Documents$
Now, we will compare two sorted file and suppress second column using -2 option with the comm command.
vikash@tutorialspoint: ~/Documents$ comm -2 file_first file_second Abhishek Anand Bruce Celesy Vikash vikash@tutorialspoint: ~/Documents$
To check more information about the comm command, we use the –help option with the comm command as shown below.
$ comm --help
To check the version information of the comm command, we use the –version option with command as shown below
$ comm --version