bohawk
bohawk

Reputation: 71

Read in file line by line and search another file for a line with a partial match

I have a file with partial matches to lines in another file. In order to do this I was looking to generate a while loop with read and substituting a variable for each line of partial matches into a grep command to search a database files with a partial match but for some reason, I am not getting an output (an empty outputfile.txt).

Here is my current script

while read -r line; do
    grep $line /path/to/databasefile >> /path/to/folder/outputfile.txt
done < "/partial_matches.txt"

the database has multiple lines with a sequence name then DNA sequence after:

 >transcript_ab
 AGTCAGTCATGTC
 >transcript_ac
 AGTCAGTCATGTC
 >transctipt_ad
 AGTCAGTCATGTC

and the partial matching search file has lines of text:

 ab
 ac

and I'm looking for a return of:

 >transcript_ab
 >transcript_ac

any help would be appreciated. Thanks.

Upvotes: 2

Views: 1275

Answers (2)

xhienne
xhienne

Reputation: 6134

If you are using GNU grep, then its -f option is what you are looking for:

grep -f /partial_matches.txt /path/to/databasefile

(if you don't have any pattern in partial_matches.txt but only strings, then use grep -F instead of grep)

Upvotes: 2

user7440787
user7440787

Reputation: 841

you can use a for loop instead:

for i in $(cat partial_matches.txt); do
grep $i /path/to/databasefile >> /path/to/folder/outputfile.txt
done

Also, check if you have a typo: "/partial_matches.txt" -> "./partial_matches.txt"

Upvotes: 1

Related Questions