Unix Command in brief
comm: compare two sorted files line by line
Syntax: comm [option]… FILE1 FILE2
- Compare sorted files FILE1 and FILE2 line by line.
- Requires two sorted files and lists differing entries in different columns. produces three text columns as output:
- Lines only in file1.
- Also, Lines only in file2.
- Lines in both files.
- suppress lines unique to FILE1
- suppress lines unique to FILE2
- suppress lines that appear in both files
In the above output we can see that first column contains two lines unique to the first file and second column contains three lines unique to the second file and the third column contains two lines common to both the files Moreover,. Comm. Can produce the single column output using 3 options -1,-2 or -3. To drop a particular column, simply use its column number as a prefix.
$ cat file1 f1.c f2.c f3.c f4.c f5.c
$ cat file2 f1.c f3.c f4.c f6.c f7.c
Now, When you run the comm command on these files, this is what you get:
$ comm file1 file2
- The output is split into 3 columns. Column1 indicates files which are unique in file1, column 2 indicates files unique to file2. Column 3 indicates files common between them. So, comm command provides some really good options with which you can filter the output better.
- Now, say you want to find out only the list of files which were there in the older version but not in the newer version:
$ comm -23 file1 file2
- Moreover, The option -23 indicates to remove the second and third columns from the comm command output, and hence we are left with only the first column which is the files unique in file1.
- Similarly, to find out the list of files which were not there in the old version, but has been added in the new version:
$ comm -13 file1 file2
- As explained above, -13 option tells to remove the first and third columns from the comm output.
- Finally, to know the list of files which have retained or common in both the versions:
$ comm -12 file1 file2
f1.c f3.c f4.c
Also, When you apply comm command on files, the files should sort. This command works only on sorted files.