如何优雅地将变量传递给命令

时间:2018-05-15 18:36:50

标签: loops sed grep genbank

我有一组当前使用一个文件的命令:

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*

(我知道,效率不高但对我有用)。它的作用很简单:它从文件ABC输出字符串LOCUS到行mainfile.gbk的行,然后输出几个sed& grep命令使文件可解析,最后将所有内容写入新文件ABC.txt

现在我想在字符串列表上迭代该命令,即

list.txt

ABC
DEF
GHI

以便从list.txt获取每一行并将其分配给变量,然后运行命令,最后为list.txt中的每一行输出一个文件。

我想把命令放在while read line循环周围,但不知何故变量的赋值不起作用/它们不会传递给命令......

1 个答案:

答案 0 :(得分:0)

如果您确定文本被格式化为单个列(没有注释或空行或任何内容),您可以像这样使用for循环。

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