Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep with a list of words

Tags:

grep

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 
like image 623
graffe Avatar asked Jul 25 '13 16:07

graffe


People also ask

How do I grep multiple word documents?

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.

How do you grep multiple items?

The basic grep syntax when searching multiple patterns in a file includes using the grep command followed by strings and the name of the file or its path. The patterns need to be enclosed using single quotes and separated by the pipe symbol. Use the backslash before pipe | for regular expressions.

How do you grep words with spaces?

For any specific space character, you just use it. If you want to allow for ANY space character (tab, space, newline, etc), then if you have a “grep” that supports EXTENDED regular expressions (with the '-E' option), you can use '[[:space:]]' to represent any space character.

How do you grep multiple lines after a match?

Use the -A argument to grep to specify how many lines beyond the match to output. And use -B n to grep lines before the match. And -C in grep to add lines both above and below the match!


2 Answers

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.

like image 59
Chris Seymour Avatar answered Sep 28 '22 06:09

Chris Seymour


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 
like image 35
Alcolo47 Avatar answered Sep 28 '22 07:09

Alcolo47