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?

  • grep -f file_with_patterns files*? – fedorqui May 3 '18 at 7:56

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.