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

比如a文件中有:

100
200
300

b文件中有:

This is 100.
That is 200.
Hello 400.

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

This is 100.
That is 200.

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

巴扎黑
巴扎黑

répondre à tous(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来处理。

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!