比较并忽略一个文件中缺少的行

Mil*_*anz 3 cygwin bash diff shell

我想比较两个文件并忽略一个文件中存在但另一个文件中缺失的行。

例如

文件1:

foo
bar
baz
bat
Run Code Online (Sandbox Code Playgroud)

文件2:

foo
ball
bat
Run Code Online (Sandbox Code Playgroud)

我当前正在运行以下 diff 命令

diff File1 File2 --changed-group-format='%>' --unchanged-group-format=''
Run Code Online (Sandbox Code Playgroud)

在这种情况下会产生

bar
baz
Run Code Online (Sandbox Code Playgroud)

作为输出,即仅缺少或冲突的行。我只想打印冲突的行,即忽略 File2 中缺少一行而 File1 中存在一行的情况(而不是相反)。有什么方法可以使用 diff 来做这样的事情还是我必须求助于其他工具?如果是这样,你会推荐什么?

小智 5

comm如果有的话,您也可以看看:

comm [-1] [-2] [-3 ] file1 file2
-1 Suppress the output column of lines unique to file1.
-2 Suppress the output column of lines unique to file2.
-3 Suppress the output column of lines duplicated in file1 and file2.
Run Code Online (Sandbox Code Playgroud)

输入文件应该排序。但是,您可以使用 --nocheck-order 选项修改默认行为(如果可用)。

在你的情况下你会想要comm --nocheck-order -23 file filter_file