从文件中查找另一个文件中不存在的行 [英] Find lines from a file which are not present in another file

查看:24
本文介绍了从文件中查找另一个文件中不存在的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件(比方说 a.txtb.txt),它们都有一个名称列表.我已经对这两个文件运行了 sort.

I have two files (let's say a.txt and b.txt), both of which has a list of names. I have already run sort on both the files.

现在我想从 a.txt 中查找 b.txt 中不存在的行.

Now I want to find lines from a.txt which are not present in b.txt.

(我花了很多时间寻找这个问题的答案,所以记录下来以备将来参考)

(I spent lot of time to find the answer for this question, so documenting it for future reference)

推荐答案

你要使用的命令不是diff而是comm

The command you have to use is not diff but comm

comm -23 a.txt b.txt

默认情况下,comm 输出 3 列:left-onlyright-onlyboth.-1-2-3 开关抑制这些列.

By default, comm outputs 3 columns: left-only, right-only, both. The -1, -2 and -3 switches suppress these columns.

所以,-23 隐藏了 right-onlyboth 列,显示只出现在第一个(左)文件中的行.

So, -23 hides the right-only and both columns, showing the lines that appear only in the first (left) file.

如果你想找到出现在两者中的行,你可以使用 -12,它隐藏了 left-onlyright-only 列,只剩下 both 列.

If you want to find lines that appear in both, you can use -12, which hides the left-only and right-only columns, leaving you with just the both column.

这篇关于从文件中查找另一个文件中不存在的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆