Grep multiple strings from text file

11,262

Solution 1

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

Solution 2

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
Share:
11,262
user3255841
Author by

user3255841

Updated on June 13, 2022

Comments

  • user3255841
    user3255841 almost 2 years

    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