如何将通信输出通过管道传输到文件? [英] How do I pipe comm outputs to a file?
问题描述
我使用了 comm 命令来比较两个文件,但我无法将它传送到第三个文件:
I've used the comm command to compare two files, but I'm unable to pipe it to a third file:
comm file1 file2 > file3
comm: file 1 is not in sorted order
comm: file 2 is not in sorted order
我该怎么做?文件已经排序.
(comm file1 file2 工作并打印出来)
(comm file1 file2 works and prints it out)
样本输入:
文件 1:
sample input:
file1:
21
24
31
36
40
87
105
134
...
文件 2:
10
21
31
36
40
40
87
103
...
comm file1 file2:有效
comm file1 file2: works
comm file1 file2 > file3
comm: file 1 is not in sorted order
comm: file 2 is not in sorted order
推荐答案
您已按数字排序;comm
适用于词法排序的文件.
You've sorted numerically; comm
works on lexically sorted files.
例如,在 file2
中,第 103 行与第 21..87 行明显乱序.您的文件必须是plain sort
排序".
For instance, in file2
, the line 103 is dramatically out of order with the lines 21..87. Your files must be 'plain sort
sorted'.
如果你有 bash
(4.x),你可以使用进程替换:
If you've got bash
(4.x), you can use process substitution:
comm <(sort file1) <(sort file2)
这会运行这两个命令并确保 comm
进程能够像读取文件一样读取它们的标准输出.
This runs the two commands and ensures that the comm
process gets to read their standard output as if they were files.
失败:
(
sort -o file1 file1 &
sort -o file2 file2 &
wait
comm file1 file2
)
这使用并行性来同时对文件进行排序.子外壳(在 ( ... )
中)确保您最终不会等待其他后台进程完成.
This uses parallelism to get the file sorted at the same time. The sub-shell (in ( ... )
) ensures that you don't end up waiting for other background processes to finish.
这篇关于如何将通信输出通过管道传输到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!