我想對文本檔案中的每個串列執行一個命令。我嘗試執行此代碼,但它沒有給我任何輸出。你能幫助我嗎?謝謝!
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在開頭移動了它。
uj5u.com熱心網友回復:
> 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 讀取回圈的輸入。
uj5u.com熱心網友回復:
跳出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
uj5u.com熱心網友回復:
也許像這樣只計算一次輸出檔案名并使用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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/424467.html
上一篇:回圈列出具有NA的檔案名
