Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep multiple strings from text file

Tags:

grep

bash

awk

Okay so I have a textfile containing multiple strings, example of this -

Hello123
Halo123
Gracias
Thank you
...

I want grep to use these strings to find lines with matching strings/keywords from other files within a directory

example of text files being grepped -

123-example-Halo123
321-example-Gracias-com-no
321-example-match

so in this instance the output should be

123-example-Halo123
321-example-Gracias-com-no
like image 777
user3255841 Avatar asked Jan 05 '18 20:01

user3255841


2 Answers

With GNU grep:

grep -f file1 file2

-f FILE: Obtain patterns from FILE, one per line.

Output:

123-example-Halo123
321-example-Gracias-com-no
like image 146
Cyrus Avatar answered Sep 21 '22 23:09

Cyrus


You should probably look at the manpage for grep to get a better understanding of what options are supported by the grep utility. However, there a number of ways to achieve what you're trying to accomplish. Here's one approach:

grep -e "Hello123" -e "Halo123" -e "Gracias" -e "Thank you" list_of_files_to_search

However, since your search strings are already in a separate file, you would probably want to use this approach:

grep -f patternFile list_of_files_to_search
like image 22
Tom Drake Avatar answered Sep 18 '22 23:09

Tom Drake