I would like to execute a command with each list from a text file. I tried doing this code but it is not giving me any output. Can you help me? Thank you!
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 looks like this
nameabc
namedef
nameghi
namejkl
while final.list looks like this
new_nameabc.fa
new_namedef.fa
new_nameghi.fa
new_namejkl.fa
In summary the long version of the code is like this and I'm trying to make a shortcut:
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
Edit: It is working now. I just replaced cat > final.list
to echo > final.list
and moved it at the beginning as suggested in the answer.
CodePudding user response:
Moving cat
out of the loop and replacing with echo
, I believe you want:
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
CodePudding user response:
Perhaps something like this to calculate the output filename only once and to use printf
instead of echo -e
for portability
#!/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
CodePudding user response:
> 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
truncates (empties) the file before it gets appended.while IFS= read -r line; ...
loop is the canonical method of processing one line of an input stream at a time. It is also far more robust compared to looping over an unquoted command substitution.< list_of_text.txt
redirects the list as input for the while read loop.