![bashのlist.txtで複数のファイル名を作成し、新しく作成されたファイルのテキストを変更する方法は? [コピー]](https://linux33.com/image/166312/bash%E3%81%AElist.txt%E3%81%A7%E8%A4%87%E6%95%B0%E3%81%AE%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%90%8D%E3%82%92%E4%BD%9C%E6%88%90%E3%81%97%E3%80%81%E6%96%B0%E3%81%97%E3%81%8F%E4%BD%9C%E6%88%90%E3%81%95%E3%82%8C%E3%81%9F%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AE%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E3%82%92%E5%A4%89%E6%9B%B4%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%E3%81%AF%EF%BC%9F%20%5B%E3%82%B3%E3%83%94%E3%83%BC%5D.png)
list.txtファイルの名前と各ファイルのテキストを使用して一度に複数のファイルを作成したいと思います。数字をlist.txt
含むファイルがあり、そのIDを名前としてid
別々のファイルを作成したいとします。EOG090W002U_M0.ctl
また、それに応じてファイルの内容も変更する必要があります。EOG090W00C1_M0.ctl
EOG090W00DC_M0.ct
サンプルlist.txt:
EOG090W002U
EOG090W00C1
EOG090W00DC
EOG090W00DE
EOG090W00E5
EOG090W00HR
EOG090W00MH
EOG090W00MS
EOG090W00PB
EOG090W00U4
EOG090W00UK
EOG090W00WM
EOG090W00WR
たとえば、ファイルの必須内容は次のとおりですEOG090W002U_M0.ctl
。EOG090W00C1_M0.ctl
seqfile = EOG090W002U_p.phy
treefile = Sametree.txt
outfile = EOG090W002U_M0_mlc
getSE = 0
RateAncestor = 1
Small_Diff = 5e-7
cleandata = 1
fix_blength = 2
method = 0
または
seqfile = EOG090W00C1_p.phy
treefile = Sametree.txt
outfile = EOG090W00C1_M0_mlc
getSE = 0
RateAncestor = 1
Small_Diff = 5e-7
cleandata = 1
fix_blength = 2
method = 0
ここで、およびseqfile
は次outfile
のように変更されますが、list.txt
ファイル内の他のテキストは変更されません。
ありがとう
答え1
while
ここでは、ドキュメントの周りにループを使用できます。
while IFS= read -r x; do
cat << EOF > "${x}_M0.ctl"
seqfile = ${x}_p.phy
treefile = Sametree.txt
outfile = ${x}_M0_mlc
getSE = 0
RateAncestor = 1
Small_Diff = 5e-7
cleandata = 1
fix_blength = 2
method = 0
EOF
done < list.txt
行にlist.txt
先行または後続のSPCまたはTAB文字がある場合は、次のことを行う必要があります。いいえファイル名の一部として解釈され(そうでない場合は変数は変更されませんIFS
)、コマンドIFS=
の前の割り当ては省略されますread
。
while read -r x; do
または、明示的にSPCとTABに設定します。
while IFS=$' \t' read -r x; do
(これを別の空白文字(たとえばCR
、、FF
... NBSP
)に拡張すると、特殊IFS空白処理は受信されず、SPC、TAB、およびNLのみが受信されるため機能しません。
答え2
#!/bin/bash
tr -d '[:blank:]' < list.txt > outputFile.tmp
for i in $(cat outputFile.tmp)
do
echo "seqfile = ${i}_p.phy" >> ${i}_M0.ctl
echo "treefile = Sametree.txt" >> ${i}_M0.ctl
echo "outfile = ${i}_M0_mlc" >> ${i}_M0.ctl
echo "" >> ${i}_M0.ctl
echo "getSE = 0" >> ${i}_M0.ctl
echo "RateAncestor = 1" >> ${i}_M0.ctl
echo "Small_Diff = 5e-7" >> ${i}_M0.ctl
echo "cleandata = 1" >> ${i}_M0.ctl
echo "fix_blength = 2" >> ${i}_M0.ctl
echo "method = 0" >> ${i}_M0.ctl
done
exit 0