Donate : Link
Medium Blog : Link
Applications : Link
comm command in the Unix family of computer operating systems is a utility that is used to compare two files for common and distinct lines.
comm reads two files as input, regarded as lines of text. comm outputs one file, which contains three columns. The first two columns contain lines unique to the first and second file, respectively. The last column contains lines common to both.
~/codeFactory$ cat test2.txt
Narendra 8
Amit 4
Rahul 20
~/codeFactory$ cat test4.txt
Narendra 8
Amit 4
Rahul 21
~/codeFactory$ comm test2.txt test4.txt
Narendra 8
Amit 4
Rahul 20
Rahul 21
~/codeFactory$ comm --help
Usage: comm [OPTION]... FILE1 FILE2
Compare sorted files FILE1 and FILE2 line by line.
When FILE1 or FILE2 (not both) is -, read standard input.
With no options, produce three-column output. Column one contains
lines unique to FILE1, column two contains lines unique to FILE2,
and column three contains lines common to both files.
-1 suppress column 1 (lines unique to FILE1)
-2 suppress column 2 (lines unique to FILE2)
-3 suppress column 3 (lines that appear in both files)
--check-order check that the input is correctly sorted, even
if all input lines are pairable
--nocheck-order do not check that the input is correctly sorted
--output-delimiter=STR separate columns with STR
--total output a summary
-z, --zero-terminated line delimiter is NUL, not newline
--help display this help and exit
--version output version information and exit
Note, comparisons honor the rules specified by 'LC_COLLATE'.
Examples:
comm -12 file1 file2 Print only lines present in both file1 and file2.
comm -3 file1 file2 Print lines in file1 not in file2, and vice versa.
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report comm translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/comm>
or available locally via: info '(coreutils) comm invocation'
Using comm with options:
1. Using -1, -2 and -3 options
~/codeFactory$ comm test2.txt test4.txt
Narendra 8
Amit 4
Rahul 20
Rahul 21
~/codeFactory$ comm -1 test2.txt test4.txt
Narendra 8
Amit 4
Rahul 21
~/codeFactory$ comm -2 test2.txt test4.txt
Narendra 8
Amit 4
Rahul 20
~/codeFactory$ comm -3 test2.txt test4.txt
Rahul 20
Rahul 21
~/codeFactory$ comm -23 test2.txt test4.txt
Rahul 20
2. Using –check-order option
This option is used to check whether the input files are sorted or not and in case if either of the two files are wrongly ordered then comm command will fail with an error message.
The above command produces the normal output if both f1.txt and f2.txt are sorted and it just gives an error message if either of the two files are not sorted.
~/codeFactory$ comm --check-order test2.txt test4.txt
Narendra 8
comm: file 1 is not in sorted order
3. Using –nocheck-order option
In case if you don’t want to check whether the input files are sorted or not, use this option. This can be explained with the help of an example.
~/codeFactory$ comm --nocheck-order test2.txt test4.txt
Narendra 8
Amit 4
Rahul 20
Rahul 21
4. –output-delimiter=STR option
By default, the columns in the comm command output are separated by spaces as explained above. However, if you want, you can change that, and have a string of your choice as separator. This can be done using the –output-delimiter option. This option requires you to specify the string that you want to use as the separator.
~/codeFactory$ comm --output-delimiter=+ test2.txt test4.txt
++Narendra 8
++Amit 4
Rahul 20
+Rahul 21
5. –total
~/codeFactory$ comm --total test2.txt test4.txt
Narendra 8
Amit 4
Rahul 20
Rahul 21
1 1 2 total

