comm: Compare Two Sorted Files
August 12th, 2024 1:42 PM Mr. Q Categories: Command
Description:
The comm
command compares two sorted files line by line and outputs three columns:
- Lines unique to the first file
- Lines unique to the second file
- Lines common to both files.
Command:
comm file1.txt file2.txt
Sample Input
file1.txt
:
apple
banana
cherry
file2.txt
:
banana
cherry
date
Sample Output:
banana
cherry
apple
date
Explanation:
- Column 1: Lines unique to
file1.txt
(apple
). - Column 2: Lines unique to
file2.txt
(date
). - Column 3: Lines common to both files (
banana
,cherry
).
Options:
-1
: Suppress the first column.-2
: Suppress the second column.-3
: Suppress the third column.
Example with Options
To display only lines that are common to both files:
comm -12 file1.txt file2.txt
Sample Output with -12
:
banana
cherry
This command is useful for comparing files, finding unique or common lines between files, and for various text processing tasks.