How to grep with a list of words

前端 未结 2 1135
后悔当初
后悔当初 2020-12-04 10:57

I have a file A with 100 words in it separated by new lines. I would like to search file B to see if ANY of the words in file A occur in it.

I tried the following bu

相关标签:
2条回答
  • 2020-12-04 11:08

    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.

    0 讨论(0)
  • 2020-12-04 11:16

    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
    
    0 讨论(0)
提交回复
热议问题