Home  >  Q&A  >  body text

shell - linux中如何筛选一个文件中的内容在另一个文件中出现过?

比如a文件中有:

100
200
300

b文件中有:

This is 100.
That is 200.
Hello 400.

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

This is 100.
That is 200.

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

巴扎黑巴扎黑2741 days ago705

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-17 16:20:09

    Execute commandgrep -f a b即可
    使用awk的话,可以这样awk 'NR==FNR{x[

    ];next}{for(i in x)if(🎜~i)print}' a b🎜

    reply
    0
  • PHPz

    PHPz2017-04-17 16:20:09

    If you want to compare the same data in two files, you can use the comm command.
    If you need to find the rows containing certain data, you still need to extract the data in file A first. For example, A is 100, 200, 300, then we can use egrep '[1-3]00' b, This is how the results came out.
    But when it comes to some more complex file comparisons, it is recommended to use shell, awk, or python to handle it.

    reply
    0
  • Cancelreply