How can I grep in a loop?

bash, grep, linux, perl, shell

Solution

There is no need to loop, you can do use `grep` with the `-f` option to get patterns from a file:

grep -f pattern_file files*

From `man grep`:

-f FILE, --file=FILE

Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX.)

Test

$ cat a1
hello
how are you?

$ cat a2
bye
hello

$ cat pattern
hello
bye

$ grep -f pattern a*
a1:hello
a2:bye
a2:hello

Problem

I have a file containing text in separate lines. ``` text1 text2 text3 textN ``` I have a directory with many files. I want to grep for each line in the of this specific directory. What is an easy way to do this?

Original source