如何抓住一个单词列表

我有一个文件 A,里面有100个单词,用新行隔开。我想搜索文件 B,看看文件 A 中的任何单词是否出现在其中。

我尝试了以下方法,但对我没有效果:

grep -F A B
136669 次浏览

You need to use the option -f:

$ grep -f A B

The option -F does a fixed string search where as -f is for specifying a file of patterns. You may want both if the file only contains fixed strings and not regexps.

$ grep -Ff A B

You may also want the -w option for matching whole words only:

$ grep -wFf A B

Read man grep for a description of all the possible arguments and what they do.

To find a very long list of words in big files, it can be more efficient to use egrep:

remove the last \n of A
$ tr '\n' '|' < A > A_regex
$ egrep -f A_regex B