# How to compare two sorted files line by line in the Linux system?

LinuxOperating SystemOpen Source

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.

## Syntax

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
1-1
Suppress first column (lines unique to FILE1)
2-2
Suppress second column (lines unique to FILE2)
3-3
Suppress third column (lines common in FILE1 and FILE2)
4--check-order
Check that the input is correctly sorted or not, even if all input lines are pairable
5--nocheck-order
Don’t check that the input is correctly sorted
6--output-delimiter=STR
Separate columns with given string STR
7--total
Display a summary
8-z, --zero-terminated
Line delimiter is NULL, not newline
9--help
Displays a help message and then exits.
10--version
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
Published on 30-Jun-2021 10:45:43
Advertisements