Search code examples
loopssedgrepgenbank

How to elegantly pass variables to command


I have a set of commands that currently work with one file:

sed -n -e '/ABC/,/LOCUS/ p' mainfile.gbk | sed -e '$ d' >temp1
sed '/     source/,/     gene/{/     gene/!d}' temp1 >temp2
grep -v "     gene" temp2 >temp3
grep -v "                     /locus_tag" temp3 >temp4
sed 's/product/locus_tag/g' temp4 >ABC.txt
echo "DONE" >>ABC.txt
rm temp*

(I know, not very efficient but works for me). Shortly what it does: it outputs lines from string ABC to line LOCUS from file mainfile.gbk, then a couple of sed & grep commands to make the file parsable, and finally writes everything to a new file ABC.txt.

Now I want to iterate that command over a list of strings, i.e.

list.txt

ABC
DEF
GHI

so that each line from list.txt is taken and a assigned to a variable, then the commands are run and finally for each line in list.txt one file is outputted.

I thought of putting the commands around a while read line loop, but somehow the assignment of the variables does not work/they are not passed to the commands...


Solution

  • If you are certain that the text is formatted as a single column (with no comments or blank lines or anything), you could use a for loop like this.

    for token in `cat list.txt`
    do
        sed -n -e "/$token/,/LOCUS/ p" mainfile.gbk | sed -e '$ d' >temp1
        sed '/     source/,/     gene/{/     gene/!d}' temp1 >temp2
        grep -v "     gene" temp2 >temp3
        grep -v "                     /locus_tag" temp3 >temp4
        sed 's/product/locus_tag/g' temp4 >$token.txt
        echo "DONE" >>$token.txt
        rm temp*
    done