0

I have a file with many strings. For simplicity, assume it is newline-delimited, and that each string is made up of only contain alphanumeric ASCII characters and underscore.

Now, I want to search a file / multiple files for occurrences of any of these strings. If there are not very many of them, I suppose I could just concatenate them with | and use a single grep. But - what if there are thousands, or tens of thousands, of them?

1
  • grep -f file_with_patterns files*? Commented May 3, 2018 at 7:56

1 Answer 1

3

You can use your file with many strings, one per line, as an argument to grep’s -f option, which tells it to look for strings listed in the named file:

grep -f patterns.txt *

(or whatever glob is appropriate). grep will find any line in the given files which match any of the patterns in patterns.txt.

You probably should specify -F too if all the strings are fixed strings to match.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.