shell - linux中如何筛选一个文件中的内容在另一个文件中出现过?
巴扎黑
巴扎黑 2017-04-17 16:18:09
0
2
701

比如a文件中有:

100
200
300

b文件中有:

This is 100.
That is 200.
Hello 400.

想取出b文件中符合条件的这两条:

This is 100.
That is 200.

单从linux文件操作的角度有好的实现方法吗?

巴扎黑
巴扎黑

Antworte allen(2)
迷茫

执行命令grep -f a b即可
使用awk的话,可以这样awk 'NR==FNR{x[$0];next}{for(i in x)if($0~i)print}' a b

PHPzhong

如果说是比较两个文件中相同的数据的话,可以使用comm命令。
如果说需要找出包含某些数据的行的话,还是要先把A文件中的数据提取出来,例如A是100,200,300,那么我们可以用egrep '[1-3]00' b,这样结果就出来了。
但是涉及到一些比较复杂文件对比,还是建议使用shell或awk、python来处理。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!