繁体   English   中英

如何将通信输出通过管道传输到文件?

[英]How do I pipe comm outputs to a file?

我已经使用 comm 命令来比较两个文件,但我无法将其通过管道传输到第三个文件:

comm file1 file2 > file3 

comm: file 1 is not in sorted order
comm: file 2 is not in sorted order

我该怎么做呢? 文件已经排序。

(comm file1 file2 工作并打印出来)

样本输入:
文件 1:

21
24
31
36
40
87
105
134
...

文件2:

10
21
31
36
40
40
87
103
...

通信文件 1 文件 2:有效

comm file1 file2 > file3 

comm: file 1 is not in sorted order
comm: file 2 is not in sorted order

你已经按数字排序了; comm适用于词法排序的文件。

例如,在file2 ,第 103 行与第 21..87 行明显乱序。 您的文件必须是“sort排序”。

如果你有bash (4.x),你可以使用进程替换:

comm <(sort file1) <(sort file2)

这将运行这两个命令并确保comm进程可以像读取文件一样读取它们的标准输出。

失败了:

(
sort -o file1 file1 &
sort -o file2 file2 &
wait
comm file1 file2
)

这使用并行性来同时对文件进行排序。 子外壳(在( ... ) )确保您最终不会等待其他后台进程完成。

您的示例数据不是按字典顺序排序的(就像在字典中一样),这是像commsort (没有-n选项)这样的命令所期望的,例如 100 应该在 20 之前。

当您不重定向输出时,您确定您不是简单地没有注意到错误消息,因为错误会与终端上的输出行混合在一起吗?

您必须先使用sort程序对文件进行sort

尝试 :

sort -o file1 file1
sort -o file2 file2
comm file1 file2 > file3

我得到的结果与您不同,但也许您的comm版本抱怨文件未按词法排序。 使用您提供的输入( ...使它变得有趣,我知道它不是您实际文件的一部分。)

$ comm file[12]
        10
                21
24
                31
                36
                40
        40
                87
        103
        ...
105
134
...

我很惊讶...不在第三列中,所以我尝试:

$ comm <(sort file1) <(sort file2)
                ...
        10
        103
105
134
                21
24
                31
                36
                40
        40
                87

那更好,但 105 > 24,对吧?

$ comm <(sort -n file1) <(sort -n file2)
                ...
        10
                21
24
                31
                36
                40
        40
                87
        103
105
134

我认为这些是您正在寻找的结果。 两个40年代也很有趣。 如果你想消除这些:

$ comm <(sort -nu file1) <(sort -nu file2)
                ...
        10
                21
24
                31
                36
                40
                87
        103
105
134

我遇到了类似的问题,即使我运行了sortcomm也在抱怨。 问题是我正在运行 Cygwin,并且sort指向某个 MSDOS 版本(我猜)。 通过使用特定路径(在我的情况下为 C:\\Cygwin\\bin\\sort),它起作用了。

我在对文件进行排序时遇到了类似的问题,但遇到了同样的错误

comm -23 16-unique.log 23-unique.log > 16-only.log

但我认为重定向无法正常工作,所以我尝试了

(comm -23 16-unique.log 23-unique.log ) > 16-only.log

但是使用排序来确保排序的输入是业务。

comm -23 <(sort 16-unique.log) <( sort 23-unique.log) > 16-only.log

[另一方面,-23 开关意味着只有第一个文件中的唯一行会在输出中] 也是man comm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM