比较Linux中的两个未排序列表,在第二个文件中列出唯一列表 [英] Comparing two unsorted lists in linux, listing the unique in the second file

查看:104
本文介绍了比较Linux中的两个未排序列表,在第二个文件中列出唯一列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件,上面有数字列表(电话号码).

I have 2 files with a list of numbers (telephone numbers).

我正在寻找一种列出第一个文件中不存在的第二个文件中数字的方法.

I'm looking for a method of listing the numbers in the second file that is not present in the first file.

我尝试了以下各种方法:

I've tried the various methods with:

comm (getting some weird sorting errors)
fgrep -v -x -f second-file.txt first-file.txt (unsure of the result, there should be more)

推荐答案

grep -Fxv -f first-file.txt second-file.txt

基本上在second-file.txt中查找与first-file.txt中的任何行都不匹配的所有行.如果文件很大,可能会变慢.

Basically looks for all lines in second-file.txt which don't match any line in first-file.txt. Might be slow if the files are large.

此外,对文件排序后(如果为数字,请使用sort -n),那么comm也应该起作用.这会带来什么错误?试试这个:

Also, once you sort the files (Use sort -n if they are numeric), then comm should also have worked. What error does it give? Try this:

comm -23 second-file-sorted.txt first-file-sorted.txt

这篇关于比较Linux中的两个未排序列表,在第二个文件中列出唯一列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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