Comm command is used to compare two sorted files line by line.
syntax: comm [OPTION]... FILE1 FILE2
This command will display the lines unique in file1, lines unique in file2, and lines common to both file1 and file2 as shown below.
In the below example,
- First column displays the lines unique in file1 ( i.e name_list.txt )
- Second column displays the lines unique in file2 ( i.e name_list_new.txt )
- Third column displays the lines that are common in both the files.
$ cat name_list.txt Bram Moolenaar Ken Thompson Linus Torvalds $ cat name_list_new.txt Bram Moolenaar Dennis Ritchie Richard Stallman $ comm name_list.txt name_list_new.txt Bram Moolenaar Dennis Ritchie Ken Thompson Linus Torvalds Richard Stallman
Comments on this entry are closed.
The real power of comm comes with the OPTIONS not explained above. The comm command can exclude columns of the output using -1, -2, or -3 where the numbers correspond to the columns described above. So to see only the lines that are in file1 but not in file2, you would run comm -23 file1 file2. Similarly to see only the lines that are in file2 but not file1, run comm -13 file1 file2. And finally, to see only the files common to both files, run comm -12 file1 file2.
Hi George
Your comments are very informative. I was trying to compare files and wanted only the output of what’s in file1 and not file2
Thanks
Owner of this thread , thanks to you too!!!
Hi Rohit,
comm -23 file1 file2
: prints only lines in the first file(file1 ) but not in the second file (file2 )
Make sure you will sort your input file’s first by using “sort” command. And then only you will use sorted output file for “comm -12 file1.sort fil2.sort”. Otherwise you will get incorrect output.
i tried with
file 1 :
1
2
3
file 2 :
1
5
6
7
comm -23 file1 file 2
1
2
3
but expected is
2
3