Question

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

  • 1 file that is a list of ~250 words I want to use as search patterns
  • 10 or so .tsv files to search individually using each of these search patterns
  • 1 file to append with the search results

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
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top