1

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.