Guide |
The Comm command in Linux allows the user to compare two sorted files by row. In this tutorial, we'll use some easy-to-understand examples to discuss this command-line tool. Before you begin, be aware that all the examples mentioned in this tutorial have been tested in Ubuntu 16.04LTS: The following example will show you how the Comm Command works. |
650) this.width=650; "src=" Http://www.linuxprobe.com/wp-content/uploads/2017/06/093734gfgib0dugxmlihd0.jpg "alt=" Use Comm to compare two sorted files "style=" Height:auto; "/>
How to compare two sorted files using Comm
To compare two sorted files using the Comm Command, simply use their names as arguments to the Comm command. The following is the usual syntax:
Comm [Name-of-first-file] [Name-of-second-file]
For example, suppose file1 and file2 are the two files in this case. The former contains the following lines of content:
001056127258
The latter contains the following lines of content:
002056167369
At this point, the output of theComm command is as follows:
Comm File1 File2
650) this.width=650; "src=" Http://www.linuxprobe.com/wp-content/uploads/2017/06/093741vqxvetv322xvr3vx-1.png "alt = "Use Comm to compare two sorted files" style= "Height:auto;"/>
As you can see, the output contains 3 columns. The first column is content that is contained only in file1 , the second column is content that is contained only in file2 , and finally, the third column is what is contained in the two files.
How to output some columns in the COMM command output
If you want, you can not output some of the columns in the Comm command output. For this feature, you have three command-line options available:-1,-2 , and -3 . As you can guess, these numbers represent the columns you don't want to output.
For example, the following command will not output the third column in the example above:
comm-3 file1 file2
650) this.width=650; "src=" Http://www.linuxprobe.com/wp-content/uploads/2017/06/093741nfa3n47qf4s8s4vt-1.png "alt = "Use Comm to compare two sorted files" style= "Height:auto;"/>
So, as you can see, the third column has no output.
Note that you can not output multiple columns of content at the same time through a single command. Like what:
comm-12 file1 file2
The above command will not output the first to second column.
How to compare two unsorted files using the Comm command
As we know,Comm can only be used for sorted files. If one of the files is found to be unsorted, then a message is generated in the output to tell the user. For example, we swap the first and second lines of the file1 and then compare them with the file2 . The following is the output of the command:
650) this.width=650; "src=" Http://www.linuxprobe.com/wp-content/uploads/2017/06/093741illmd1xltay1k2hc-1.png "alt = "Use Comm to compare two sorted files" style= "Height:auto;"/>
As you can see, this command produces an output that tells us:File1 is not yet sorted. At this point, if you do not want the tool to check that the input is sorted, then you can use the --nocheck-order option:
Comm--nocheck-order File1 File2
650) this.width=650; "src=" Http://www.linuxprobe.com/wp-content/uploads/2017/06/093741vx5xjew8j1nc511j-1.png "alt = "Use Comm to compare two sorted files" style= "Height:auto;"/>
As you can see, the preceding message has disappeared.
Note that if you want to explicitly tell the Comm command to check if the input files are sorted, then you can use the --check-order option.
How to separate the output columns of the Comm command with a custom string
By default, the output columns of theComm Command are separated by a space. However, how do you want to use a custom string as a delimiter, then you can use the --output-delimiter option. You need to specify the string you want to use as a delimiter when using this option.
Comm--output-delimiter=+ File1 File2
For example, we use the plus sign as a delimiter:
650) this.width=650; "src=" Http://www.linuxprobe.com/wp-content/uploads/2017/06/093742yqq2pw3e3m3iimqi-1.png "alt = "Use Comm to compare two sorted files" style= "Height:auto;"/>
How to make Comm output lines terminate with NUL characters
By default, the output line of theComm command terminates with a new line. However, if you want to, then you can terminate with a NUL
character instead, just use the- z option:
Comm-z file1 file2
Conclusion
The Comm command is not particularly feature-specific, and we have discussed most of its command-line options here. Just understand and practice what's discussed in this tutorial, and you'll know how to use the tool in your daily work. If you have any questions or concerns, please go to the Man Manual of the order, or comment below.
Original address: http://www.linuxprobe.com/comm-sort.html
This article is from the "blog" blog, please be sure to keep this source http://coderhsf.blog.51cto.com/12629645/1942375
Method for comparing two sorted files--comm