给定一个包含搜索词的文件;
> cat words.txt
aaa
bbb
ccc
要搜索的句子列表;
> cat sent.txt
aaa is 1
bbb is 2
ccc is 3
aaa and bbb is 4
aaa and ccc is 5
我想获得与任何一个搜索字词匹配的行列表,其中每次出现搜索字词时都会显示一行。例如,以上将返回;
> # Some Command
aaa is 1
bbb is 2
ccc is 3
aaa and bbb is 4 # matches 'aaa'
aaa and bbb is 4 # matches 'bbb'
aaa and ccc is 5 # matches 'aaa'
aaa and ccc is 5 # matches 'ccc'
查看grep
文档,我认为我是使用-o
的方式的一部分;
> cat sent.txt | grep -f words.txt -o
aaa
bbb
ccc
aaa
bbb
aaa
ccc
显示匹配每个句子的搜索词。但是,是否可以确保返回句子,而不必遍历每个搜索词?