使用文本文件中的每个列表执行命令
execute a command with each list from a text file
我想对文本文件中的每个列表执行一个命令。我尝试执行此代码,但它没有给我任何输出。你能帮助我吗?谢谢!
for i in $(cat list_of_text.txt)
do
commandtool some_input_file $i> new_"$i".fa
cat > final.list
echo -e "new_"$i"\t+" >> final.list
done
list_of_text.txt 看起来像这样
nameabc
namedef
nameghi
namejkl
而 final.list 看起来像这样
new_nameabc.fa +
new_namedef.fa +
new_nameghi.fa +
new_namejkl.fa +
总结
代码的长版本是这样的,我正在尝试创建一个快捷方式:
commandtool some_input_file nameabc> new_nameabc.fa
commandtool some_input_file namedef> new_namedef.fa
commandtool some_input_file nameghi> new_nameghi.fa
commandtool some_input_file namejkl> new_namejkl.fa
echo -e "new_nameabc.fa\t+" > final.list
echo -e "new_namedef.fa\t+" >> final.list
echo -e "new_nameghi.fa\t+" >> final.list
echo -e "new_namejkl.fa\t+" >> final.list
编辑:它现在正在运行。我只是将 cat > final.list
替换为 echo > final.list
并按照答案中的建议将其移动到开头。
将 cat
移出循环并替换为 echo
,我相信你想要:
echo > final.list
for i in $(cat list_of_text.txt)
do
commandtool some_input_file $i > new_"$i".fa
echo -e "new_${i}.fa\t+" >> final.list
done
也许像这样只计算一次输出文件名并使用 printf
而不是 echo -e
以实现可移植性
#!/bin/bash
true > final.list
for i in $(< list_of_text.txt)
do
out="new_${i}.fa"
commandtool some_input_file "$i" > "$out"
printf '%s\t+\n' "$out" >> final.list
done
> final.list
while IFS= read -r name; do
new_name=new_${name}.fa
cmd input-file "$name" > "$new_name"
printf '%s\t\n' "$new_name" >> final.list
done < list_of_text.txt
> final.list
在追加之前截断(清空)文件。
while IFS= read -r line; ...
循环是一次处理一行输入流的规范方法。与遍历未引用的命令替换相比,它也更加健壮。
< list_of_text.txt
将列表重定向为 while read 循环的输入。
我想对文本文件中的每个列表执行一个命令。我尝试执行此代码,但它没有给我任何输出。你能帮助我吗?谢谢!
for i in $(cat list_of_text.txt)
do
commandtool some_input_file $i> new_"$i".fa
cat > final.list
echo -e "new_"$i"\t+" >> final.list
done
list_of_text.txt 看起来像这样
nameabc
namedef
nameghi
namejkl
而 final.list 看起来像这样
new_nameabc.fa +
new_namedef.fa +
new_nameghi.fa +
new_namejkl.fa +
总结 代码的长版本是这样的,我正在尝试创建一个快捷方式:
commandtool some_input_file nameabc> new_nameabc.fa
commandtool some_input_file namedef> new_namedef.fa
commandtool some_input_file nameghi> new_nameghi.fa
commandtool some_input_file namejkl> new_namejkl.fa
echo -e "new_nameabc.fa\t+" > final.list
echo -e "new_namedef.fa\t+" >> final.list
echo -e "new_nameghi.fa\t+" >> final.list
echo -e "new_namejkl.fa\t+" >> final.list
编辑:它现在正在运行。我只是将 cat > final.list
替换为 echo > final.list
并按照答案中的建议将其移动到开头。
将 cat
移出循环并替换为 echo
,我相信你想要:
echo > final.list
for i in $(cat list_of_text.txt)
do
commandtool some_input_file $i > new_"$i".fa
echo -e "new_${i}.fa\t+" >> final.list
done
也许像这样只计算一次输出文件名并使用 printf
而不是 echo -e
以实现可移植性
#!/bin/bash
true > final.list
for i in $(< list_of_text.txt)
do
out="new_${i}.fa"
commandtool some_input_file "$i" > "$out"
printf '%s\t+\n' "$out" >> final.list
done
> final.list
while IFS= read -r name; do
new_name=new_${name}.fa
cmd input-file "$name" > "$new_name"
printf '%s\t\n' "$new_name" >> final.list
done < list_of_text.txt
> final.list
在追加之前截断(清空)文件。while IFS= read -r line; ...
循环是一次处理一行输入流的规范方法。与遍历未引用的命令替换相比,它也更加健壮。< list_of_text.txt
将列表重定向为 while read 循环的输入。