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

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

问题描述

因此,我有一个带有名称列表的file1.txt,另一个带有另一个名称列表的file2.txt,我需要一个包含两个文件名称的列表。



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



有没有人知道为什么会发生这种情况,我能做些什么才能得到两个名单上的名字?



谢谢。

解决方案

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

  comm -12 file1.txt file2.txt> newlist.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.

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?

thank you.

解决方案

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

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

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

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