6月 06

linux 两个文件取交集命令

  有两个文件a和b,a中包含b中所有数据。需要找出a文件中包含b文件内容的数据。具体操作如下:
a.txt文件
http://www.simonzhang.net/msg?phone=12320421&msg=hfeaf
http://www.simonzhang.net/msg?phone=13219543&msg=efweff3
http://www.simonzhang.net/msg?phone=12765745&msg=f3fds
http://www.simonzhang.net/msg?phone=12432321&msg=3r2r322
http://www.simonzhang.net/msg?phone=14142142&msg=rgewo3
http://www.simonzhang.net/msg?phone=14212412&msg=iv9e
http://www.simonzhang.net/msg?phone=12321243&msg=e

b.txt文件
12320421
12432321
12321243

  查找a对b的交集命令:grep -f b.txt a.txt
结果如下:
http://www.baidu.com/msg?phone=12320421&msg=hfeaf
http://www.baidu.com/msg?phone=12432321&msg=3r2r322
http://www.baidu.com/msg?phone=12321243&msg=e

举一反三,如果要找差集命令为:grep -v -f b.txt a.txt