在另一个文件中查找行 [英] find lines from one file in another

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

问题描述

所以我有一个包含名称列表的 file1.txt 和一个包含另一个名称列表的 file2.txt,我需要一个包含两个文件中名称的列表.

So I have a file1.txt with a list of names, and a file2.txt with another list of names and I need a list with the names that are in both files.

我试过 grep -f file1.txt file2.txt >newlist.txt 但由于某种原因它不起作用,并且 newlist.txt 的名称不在 file1 中.

I tried grep-f file1.txt file2.txt > newlist.txt but for some reason it isn't working, and the newlist.txt has names that are not in file1.

有谁知道为什么会发生这种情况以及我可以做些什么来只获取两个列表中的名称?

Does anyone know why this is happening and what i could do to get only the names that are on both lists?

谢谢.

推荐答案

如果 file1.txt 和 file2.txt 已排序,则可以使用 'comm'

If file1.txt and file2.txt are sorted, you could use 'comm'

comm -12 file1.txt file2.txt > newlist.txt

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

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