2019-04-21 11:50:48 +08:00

116 lines
4.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1 id="comm">comm</h1>
<p>两个文件之间的比较</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>comm命令</strong> 可以用于两个文件之间的比较,它有一些选项可以用来调整输出,以便执行交集、求差、以及差集操作。</p>
<ul>
<li>交集:打印出两个文件所共有的行。</li>
<li>求差:打印出指定文件所包含的且不相同的行。</li>
<li>差集:打印出包含在一个文件中,但不包含在其他指定文件中的行。</li>
</ul>
<h3 id="语法">语法</h3>
<div class="sourceCode" id="cb1"><pre><code class="language-bash"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="fu">comm</span> [选项]... 文件1 文件2</a></code></pre></div>
<h3 id="选项">选项</h3>
<pre><code class="language-bash">如果不附带选项,程序会生成三列输出。
第一列包含文件1 特有的行,
第二列包含文件2 特有的行,
而第三列包含两个文件共有的行。
-1 不输出文件1 特有的行
-2 不输出文件2 特有的行
-3 不输出两个文件共有的行
--check-order 检查输入是否被正确排序,即使所有输入行均成对
--nocheck-order 不检查输入是否被正确排序
--output-delimiter=STR 依照STR 分列</code></pre>
<h3 id="参数">参数</h3>
<ul>
<li>文件1指定要比较的第一个<strong>有序</strong>文件;</li>
<li>文件2指定要比较的第二个<strong>有序</strong>文件。</li>
</ul>
<h3 id="实例">实例</h3>
<p>文本 <code>aaa.txt</code> 内容</p>
<pre><code class="language-bash">[root@localhost text]# cat aaa.txt
aaa
bbb
ccc
ddd
eee
111
222</code></pre>
<p>文本 <code>bbb.txt</code> 内容</p>
<pre><code class="language-bash">[root@localhost text]# cat bbb.txt
bbb
ccc
aaa
hhh
ttt
jjj</code></pre>
<p>两个文件之间的比较,如果没有排序需要带上<code>--nocheck-order</code>参数, 没有带上参数将会收到提示,此命令重要之功能在于比较。</p>
<div class="sourceCode" id="cb5"><pre><code class="language-bash"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="ex">comm</span>: 文件2 没有被正确排序</a>
<a class="sourceLine" id="cb5-2" data-line-number="2"><span class="ex">comm</span>: 文件1 没有被正确排序</a></code></pre></div>
<p>比较结果</p>
<pre><code class="language-bash">[root@localhost text]# comm --nocheck-order aaa.txt bbb.txt
aaa
bbb
ccc
aaa
ddd
eee
111
222
hhh
ttt
jjj
第一列 第二列 第三列</code></pre>
<p>输出的第一列只包含在aaa.txt中出现的行第二列包含在bbb.txt中出现的行第三列包含在aaa.txt和bbb.txt中相同的行。各列是以制表符作为定界符。</p>
<h3 id="有序比较">有序比较</h3>
<p>有序比较,先通过 sort 将文件内容排序</p>
<pre><code class="language-bash">[root@localhost ~]# sort aaa.txt &gt; aaa1.txt
[root@localhost ~]# sort bbb.txt &gt; bbb1.txt</code></pre>
<p>有序比较结果:</p>
<pre><code class="language-bash">[root@localhost ~]# comm aaa1.txt bbb1.txt
111
222
aaa
bbb
ccc
ddd
eee
hhh
jjj
ttt</code></pre>
<h3 id="交集">交集</h3>
<p>打印两个文件的交集,需要删除第一列和第二列:</p>
<pre><code class="language-bash">[root@localhost text]# comm aaa.txt bbb.txt -1 -2
bbb
ccc</code></pre>
<p><strong>求差</strong></p>
<p>打印出两个文件中不相同的行,需要删除第三列:</p>
<pre><code class="language-bash">[root@localhost text]# comm aaa.txt bbb.txt -3 | sed &#39;s/^\t//&#39;
aaa
aaa
ddd
eee
111
222
hhh
ttt
jjj</code></pre>
<p><code>sed 's/^\t//'</code> 是将制表符<code>\t</code>删除,以便把两列合并成一列。</p>
<h3 id="差集">差集</h3>
<p>通过删除不需要的列可以得到aaa.txt和bbb.txt的差集</p>
<p>aaa.txt的差集</p>
<pre><code class="language-bash">[root@localhost text]# comm aaa.txt bbb.txt -2 -3
aaa
ddd
eee
111
222</code></pre>
<p>bbb.txt的差集</p>
<pre><code class="language-bash">[root@localhost text]# comm aaa.txt bbb.txt -1 -3
aaa
hhh
ttt
jjj</code></pre>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->