mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-23 21:20:42 +08:00
Translated
This commit is contained in:
parent
9d34c5f335
commit
87338571f9
@ -1,150 +0,0 @@
|
||||
ucasFL translating
|
||||
Linux comm command tutorial for beginners (5 examples)
|
||||
============================================================
|
||||
|
||||
### On this page
|
||||
|
||||
1. [Linux comm command][1]
|
||||
|
||||
2. [1\. How to compare two sorted files using comm][2]
|
||||
|
||||
3. [2\. How to suppress individual columns in comm command output][3]
|
||||
|
||||
4. [3\. How to make comm compare files that are not sorted][4]
|
||||
|
||||
5. [4\. How to separate comm output columns with custom string][5]
|
||||
|
||||
6. [5\. How to make comm output lines NUL terminated][6]
|
||||
|
||||
7. [Conclusion][7]
|
||||
|
||||
The **comm** command in Linux lets users compare two sorted files line by line. In this tutorial, we will discuss this command line tool using easy to understand examples. But before we do that, please note that all examples mentioned in this tutorial have been tested on Ubuntu 16.04LTS.
|
||||
|
||||
### Linux comm command
|
||||
|
||||
The following examples should give you a nice idea about how the 'comm' tool works.
|
||||
|
||||
### 1\. How to compare two sorted files using comm
|
||||
|
||||
To compare two sorted files using 'comm', all you have to do is to pass their names as arguments to the command line tool. Here's the generic syntax:
|
||||
|
||||
```
|
||||
comm [name-of-first-file] [name-of-second-file]
|
||||
```
|
||||
|
||||
For example, suppose 'file1' and 'file2' are the two files in our case. The former contains the following lines:
|
||||
|
||||
```
|
||||
001
|
||||
056
|
||||
127
|
||||
258
|
||||
```
|
||||
|
||||
while the latter contains the following lines:
|
||||
|
||||
```
|
||||
002
|
||||
056
|
||||
167
|
||||
369
|
||||
```
|
||||
|
||||
Now, here's the 'comm' command output in this case:
|
||||
|
||||
```
|
||||
comm file1 file2
|
||||
```
|
||||
|
||||
[![](https://www.howtoforge.com/images/linux_comm_command/comm-output.png)][8]
|
||||
|
||||
So you can see that output consists of three columns. The first one contains lines unique to 'file1', second one contains lines unique to 'file2', and finally, column three contains lines common to both files.
|
||||
|
||||
### 2\. How to suppress individual columns in comm command output
|
||||
|
||||
If you want, you can suppress individual columns in comm command output. For this, you have three command line options: **-1**, **-2**, and **-3**. As you'd have guessed, these numbers represents the columns you want to suppress.
|
||||
|
||||
For example, here's the command to suppress the third column in our case:
|
||||
|
||||
```
|
||||
comm -3 file1 file2
|
||||
```
|
||||
|
||||
[![](https://www.howtoforge.com/images/linux_comm_command/comm-supress-column.png)][9]
|
||||
|
||||
So you can see that the third column got suppressed.
|
||||
|
||||
Note that you can suppress multiple columns with a single command. For example:
|
||||
|
||||
```
|
||||
comm -12 file1 file2
|
||||
```
|
||||
|
||||
will suppress both first and second columns.
|
||||
|
||||
### 3\. How to make comm compare files that are not sorted
|
||||
|
||||
As we know, the comm command only works with sorted files. If it finds that a file is not sorted, a message is produced in the output that tells the user about this. For example, we swapped the first and the second lines in 'file1,' and then compared it with 'file2.' Here's what the output was:
|
||||
|
||||
[![](https://www.howtoforge.com/images/linux_comm_command/comm-not-sorted-message.png)][10]
|
||||
|
||||
So you can see that the command produced an output saying 'file1' is not sorted. Now, if you don't want the tool to check whether or not the input is sorted, you can use the **--nocheck-order** option.
|
||||
|
||||
```
|
||||
comm --nocheck-order file1 file2
|
||||
```
|
||||
|
||||
[![](https://www.howtoforge.com/images/linux_comm_command/comm-nocheck-order.png)][11]
|
||||
|
||||
So you can see that the message that was earlier being displayed got suppressed.
|
||||
|
||||
Please note that just in case you want to explicitly tell the comm command to carry out the sorting check on input files, you can use the **--check-order** option.
|
||||
|
||||
### 4\. How to separate comm output columns with custom string
|
||||
|
||||
By default, the columns in the comm command output are separated by spaces. 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.
|
||||
|
||||
```
|
||||
comm --output-delimiter=STR file1 file2
|
||||
```
|
||||
|
||||
For example, we used the plus (+) symbol as the delimiter.
|
||||
|
||||
[![](https://www.howtoforge.com/images/linux_comm_command/comm-output-delimiter.png)][12]
|
||||
|
||||
### 5\. How to make comm output lines NUL terminated
|
||||
|
||||
The comm command output lines are newline-terminated by default. However, if you want, you can make them NUL terminated instead. This can be done using the **-z** command line option.
|
||||
|
||||
```
|
||||
comm -z file1 file2
|
||||
```
|
||||
|
||||
### Conclusion
|
||||
|
||||
The comm command doesn't offer a whole lot of features - we've covered almost all its command line options here. So just understand and practice what all we've discussed in this tutorial, and you'll be ready to use the tool in your day-to-day tasks. In case of any doubt or query, head to [the command's man page][13], or leave a comment below.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.howtoforge.com/linux-comm-command/
|
||||
|
||||
作者:[Himanshu Arora ][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.howtoforge.com/linux-comm-command/
|
||||
[1]:https://www.howtoforge.com/linux-comm-command/#linux-comm-command
|
||||
[2]:https://www.howtoforge.com/linux-comm-command/#-how-to-compare-two-sorted-files-using-comm
|
||||
[3]:https://www.howtoforge.com/linux-comm-command/#-how-tonbspsuppress-individual-columns-in-comm-command-output
|
||||
[4]:https://www.howtoforge.com/linux-comm-command/#-how-to-make-comm-compare-files-that-are-not-sorted
|
||||
[5]:https://www.howtoforge.com/linux-comm-command/#-how-to-separate-comm-output-columns-with-custom-string
|
||||
[6]:https://www.howtoforge.com/linux-comm-command/#-how-to-make-comm-output-lines-nul-terminated
|
||||
[7]:https://www.howtoforge.com/linux-comm-command/#conclusion
|
||||
[8]:https://www.howtoforge.com/images/linux_comm_command/big/comm-output.png
|
||||
[9]:https://www.howtoforge.com/images/linux_comm_command/big/comm-supress-column.png
|
||||
[10]:https://www.howtoforge.com/images/linux_comm_command/big/comm-not-sorted-message.png
|
||||
[11]:https://www.howtoforge.com/images/linux_comm_command/big/comm-nocheck-order.png
|
||||
[12]:https://www.howtoforge.com/images/linux_comm_command/big/comm-output-delimiter.png
|
||||
[13]:https://linux.die.net/man/1/comm
|
@ -0,0 +1,133 @@
|
||||
针对新手的 Linux `comm` 命令教程
|
||||
===========================================================
|
||||
|
||||
Linux 中的 `comm` 命令可以让用户按行比较两个已经排序好的文件。在本教程中,我们将使用一些浅显易懂的例子来讨论这个命令行工具。在开始之前,请注意,本教程中提到的所有例子都已经在 Ubuntu 16.04LTS 版本中测试过。
|
||||
|
||||
### Linux `comm` 命令
|
||||
|
||||
下面的例子将会告诉你 `comm` 命令是如何工作的。
|
||||
|
||||
### 1\. 如何使用 `comm` 比较两个排序好的文件
|
||||
|
||||
要使用 `comm` 命令比较两个排序好的文件,只需要把它们的名字作为 `comm` 命令的参数。下面是通常的语法:
|
||||
|
||||
```
|
||||
comm [name-of-first-file] [name-of-second-file]
|
||||
```
|
||||
|
||||
比如,假设 file1 和 file2 是这种情况下的两个文件。前者包含下面几行内容:
|
||||
|
||||
```
|
||||
001
|
||||
056
|
||||
127
|
||||
258
|
||||
```
|
||||
|
||||
而后者包含下面几行内容:
|
||||
|
||||
```
|
||||
002
|
||||
056
|
||||
167
|
||||
369
|
||||
```
|
||||
|
||||
此时,`comm` 命令的输出如下图所示:
|
||||
|
||||
```
|
||||
comm file1 file2
|
||||
```
|
||||
|
||||
[![](https://www.howtoforge.com/images/linux_comm_command/comm-output.png)][8]
|
||||
|
||||
你可以看到,输出包含 3 列。第一列是仅包含在 file1 中的内容,第二列是仅包含在 file2 中的内容,最后,第三列是两个文件中均包含的内容。
|
||||
|
||||
### 2\. 如何不输出 `comm` 命令输出中的某些列
|
||||
|
||||
如果你想,你可以不输出 `comm` 命令输出中的某些列。对于该特性,你有三个命令行选项可用:**-1**、**-2** 和 **-3** 。正如你所猜想的,这些数字表示你不想输出的列。
|
||||
|
||||
比如,下面这个命令将会不输出上面例子中的第三列:
|
||||
|
||||
```
|
||||
comm -3 file1 file2
|
||||
```
|
||||
[![](https://www.howtoforge.com/images/linux_comm_command/comm-supress-column.png)][9]
|
||||
|
||||
|
||||
因此,你可以看到,第三列并没有输出。
|
||||
|
||||
注意,你可以通过一个单一命令同时不输出多列内容。比如:
|
||||
|
||||
```
|
||||
comm -12 file1 file2
|
||||
```
|
||||
|
||||
上面这个命令将会不输出第一、二列。
|
||||
|
||||
### 3\. 如何使用 `comm` 命令比较两个未排序好的文件
|
||||
|
||||
正如我们所知道的,`comm` 只可用于排序好的文件。如果发现其中一个文件未排序好,那么便会在输出中产生一条信息来告诉用户。比如,我们交换 file1 的第一行和第二行,然后与 file2 进行比较。下面是该命令的输出:
|
||||
|
||||
[![](https://www.howtoforge.com/images/linux_comm_command/comm-not-sorted-message.png)][10]
|
||||
|
||||
你可以看到,这个命令产生一个输出告诉我们:file1 还没有排序好。此时,如果你不想让这个工具检查输入是否已经排序好,那么你可以使用 `--nocheck-order` 选项:
|
||||
|
||||
```
|
||||
comm --nocheck-order file1 file2
|
||||
```
|
||||
|
||||
[![](https://www.howtoforge.com/images/linux_comm_command/comm-nocheck-order.png)][11]
|
||||
|
||||
你可以看到,前面出现的提示信息已经消失了。
|
||||
|
||||
注意,如果你想明确告诉 `comm` 命令来检查输入文件是否排序好,那么你可以使用 `--check-order` 选项。
|
||||
|
||||
### 4\. 如何用自定义字符串分隔 `comm` 命令的输出列
|
||||
|
||||
默认情况下,`comm` 命令的输出列之间是以空格分隔的。然而,如何你想使用一个自定义字符串作为分隔符,那么你可以使用 `--output-delimiter` 选项。使用该选项时需要指定你想用来作为分隔符的字符串。
|
||||
|
||||
```
|
||||
comm --output-delimiter=STR file1 file2
|
||||
```
|
||||
|
||||
比如,我们使用加号来作为分隔符:
|
||||
|
||||
[![](https://www.howtoforge.com/images/linux_comm_command/comm-output-delimiter.png)][12]
|
||||
|
||||
### 5\. 如何使 `comm` 的输出行以空字符终止
|
||||
|
||||
默认情况下,`comm` 命令的输出行以新行终止。然而,如果你想,那么你可以改为以空字符终止,只需要使用 `-z` 选项即可:
|
||||
|
||||
```
|
||||
comm -z file1 file2
|
||||
```
|
||||
|
||||
### 结论
|
||||
|
||||
`comm` 命令并没有特别多的特性性,我们在这儿已经讨论了它的绝大多数命令行选项。只需要理解和练习在这篇教程中讨论的内容,那么你便可以在日常工作中知道如何使用这个工具了。如果你有任何问题或者疑问,请前往该命令的 [man 手册][13],或者在下面评论。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.howtoforge.com/linux-comm-command/
|
||||
|
||||
作者:[Himanshu Arora][a]
|
||||
译者:[ucasFL](https://github.com/ucasFL)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.howtoforge.com/linux-comm-command/
|
||||
[1]:https://www.howtoforge.com/linux-comm-command/#linux-comm-command
|
||||
[2]:https://www.howtoforge.com/linux-comm-command/#-how-to-compare-two-sorted-files-using-comm
|
||||
[3]:https://www.howtoforge.com/linux-comm-command/#-how-tonbspsuppress-individual-columns-in-comm-command-output
|
||||
[4]:https://www.howtoforge.com/linux-comm-command/#-how-to-make-comm-compare-files-that-are-not-sorted
|
||||
[5]:https://www.howtoforge.com/linux-comm-command/#-how-to-separate-comm-output-columns-with-custom-string
|
||||
[6]:https://www.howtoforge.com/linux-comm-command/#-how-to-make-comm-output-lines-nul-terminated
|
||||
[7]:https://www.howtoforge.com/linux-comm-command/#conclusion
|
||||
[8]:https://www.howtoforge.com/images/linux_comm_command/big/comm-output.png
|
||||
[9]:https://www.howtoforge.com/images/linux_comm_command/big/comm-supress-column.png
|
||||
[10]:https://www.howtoforge.com/images/linux_comm_command/big/comm-not-sorted-message.png
|
||||
[11]:https://www.howtoforge.com/images/linux_comm_command/big/comm-nocheck-order.png
|
||||
[12]:https://www.howtoforge.com/images/linux_comm_command/big/comm-output-delimiter.png
|
||||
[13]:https://linux.die.net/man/1/comm
|
Loading…
Reference in New Issue
Block a user