Search code examples
grep

How to grep with a list of words


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 but does not work to me:

grep -F A B

Solution

  • 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.