This is a test version of Biostars. For the public version, visit https://www.biostars.org.
searching for genes included in the file name

Dear all,

i have a list of of files,

TMCS09g1008676.gene_ancestors.txt
TMCS09g1008677.gene_ancestors.txt
TMCS09g1008677.gene_ancestors.txt
TMCS09g1008679.gene_ancestors.txt
TMCS09g1008680.gene_ancestors.txt
TMCS09g100868.gene_ancestors.txt

what i want to do is to search for that specific ID in this files in order to filter them. Should be a simple e.g. grep TMCS09g1008676 TMCS09g1008676.gene_ancestors.txt, etc.. but i want to automatize all. i tried creating a list and then iterating e.g. echo TMCS09g1008676.gene_ancestors.txt | cut d '.' -f1 | grep - TMCS09g1008676.gene_ancestors.txt but then grep cannot read from its standard input. any help would be highly appreciated. thanks

bash

2 answers

What about a loop like:

for file in *.gene_ancestors.txt
do
gene=$(echo $file | cut -f1 -d'.')
grep $gene $file
done

my thought exactly.

however, perhaps you'll need to be a bit more strict with grep , as for example TMCS09g100868 might also return TMCS09g1008680. This of course depends also on the content of the files . If there is no confusion possible then WouterDeCoster answer will do perfectly.

perfectly right. grep -w would be even better

I cant believe it, WouterDeCoster has written a useful answer! O.o.

Or you can use while in bash. But if your files are big, this is not very fast solution.

while read genes; 
 do
 echo "----SEARCHING: $genes"
grep -w "$genes" *.gene_ancestors.txt >> result

done < gene_list.txt

Log in to answer this question.