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

use*_*479 1 unix

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

comm file1 file2 > file3 

comm: file 1 is not in sorted order
comm: file 2 is not in sorted order
Run Code Online (Sandbox Code Playgroud)

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

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

示例输入:
file1:

21
24
31
36
40
87
105
134
...
Run Code Online (Sandbox Code Playgroud)

文件2:

10
21
31
36
40
40
87
103
...
Run Code Online (Sandbox Code Playgroud)

通信文件 1 文件 2:有效

comm file1 file2 > file3 

comm: file 1 is not in sorted order
comm: file 2 is not in sorted order
Run Code Online (Sandbox Code Playgroud)

Jon*_*ler 6

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

例如,在 中file2,第 103 行与第 21..87 行严重失序。您的文件必须是“sort排序”。

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

comm <(sort file1) <(sort file2)
Run Code Online (Sandbox Code Playgroud)

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

失败了:

(
sort -o file1 file1 &
sort -o file2 file2 &
wait
comm file1 file2
)
Run Code Online (Sandbox Code Playgroud)

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