linux 中 grep -f选项的作用

 

1、测试数据

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
b
c
d
e
root@PC1:/home/test4# cat b.txt
c
d
d
xef
e
f
g

 

2、grep -f选项的作用表示以file1中的每个元素在file2中进行匹配

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
b
c
d
e
root@PC1:/home/test4# cat b.txt
c
d
d
xef
e
f
g
root@PC1:/home/test4# grep -f a.txt b.txt    ## 以a.txt每一行为元素, 在b.txt中进行匹配
c
d
d
xef
e
root@PC1:/home/test4# for i in $(cat a.txt); do grep $i b.txt ; done    ## 验证
c
d
d
xef
e
root@PC1:/home/test4# grep -f b.txt a.txt        ## 以b.txt每一行为元素, 在a.txt中进行匹配
c
d
e
root@PC1:/home/test4# for i in $(cat b.txt); do grep $i a.txt ; done   ## 验证,可见grep -f匹配前进行了去重复
c
d
d
e

 

posted @ 2022-05-27 11:30  小鲨鱼2018  阅读(2942)  评论(0)    收藏  举报