简体   繁体   中英

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.

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 )

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM