Slavatron
Slavatron

Reputation: 2358

Bash Perform multiple grep searches using patterns stored in a file

Working in the default Unix environment that comes with Mac computers. Environment contains the following:

I want to run the following script:

for file in *.tsv; 
do 
echo "Processing $file"; 
grep -n PATTERN $file | cut -f 1,2,3 >> Results_File.lst; 
done

But I do not know how to run the script repeatedly for multiple patterns stored in a separate file.

Here is a small slice of the patterns file:

    AXDND1
    BAZ2B
    BBS10
    BRIP1
    etc

Upvotes: 1

Views: 707

Answers (1)

hek2mgl
hek2mgl

Reputation: 157967

You don't need the for loop. You can pass the *.tsv glob to grep and it will search in all of those files. If you pass the option -f pattern.txt to grep it will search for all patterns in pattern.txt.

The following command should do the whole work:

grep -n -f pattern.txt *.tsv | cut -f 1,2,3 >> Results_File.lst

Check man grep for further explanation of grep and it's options.

Upvotes: 2

Related Questions