Displaying Unique Lines in Linux using the comm
Command
The comm
command in Linux is a powerful tool for comparing and displaying unique lines between two sorted text files. It can be particularly useful when you need to identify the unique lines in a file or compare the contents of two files.
Understanding the comm
Command
The comm
command takes two input files, compares them, and outputs three columns:
- Column 1: Lines that are unique to the first file.
- Column 2: Lines that are unique to the second file.
- Column 3: Lines that are common to both files.
By default, the comm
command expects the input files to be sorted. If the files are not sorted, you can sort them using the sort
command before using comm
.
Here's the basic syntax for the comm
command:
comm [options] file1 file2
The most common options for the comm
command are:
-1
: Suppress the display of column 1 (lines unique to the first file).-2
: Suppress the display of column 2 (lines unique to the second file).-3
: Suppress the display of column 3 (lines common to both files).
Displaying Unique Lines Using comm
To display the unique lines in a single file using the comm
command, you can use the following command:
comm -23 file1.txt file1.txt
This command will display the lines that are unique to the first file (file1.txt
). The -23
option suppresses the display of columns 2 and 3, leaving only the lines that are unique to the first file.
If you want to compare the unique lines between two files, you can use the following command:
comm -12 file1.txt file2.txt
This command will display the lines that are unique to both file1.txt
and file2.txt
. The -12
option suppresses the display of columns 1 and 3, leaving only the lines that are unique to both files.
Here's a Mermaid diagram that illustrates the concept:
By using the comm
command, you can easily identify and display the unique lines in your text files, which can be particularly useful when working with large datasets or comparing the contents of multiple files.