画像と*.txtファイルを添付しましたhttps://1drv.ms/t/s!Aoomvi55MLAQh1jODfUxa-xurns_ サンプルワークシートの一部です。このファイルでは、応答は「r1f」、「r2f」、「r3f」などで始まります。各反応について、反応速度は数行後に表示され、前に「+」記号が表示されます。応答率の最初と3番目の数字を+/-75%に変更したいと思います。したがって、各反応には4つの変形値があります。したがって、Prob01.txtファイルに6つの反応がある場合は、それぞれ1つの反応速度の変更しかない6 * 4 = 24 txtファイルを持ちたいと思います。つまり、最初のレスポンスのみに、レスポンス1の4つの変更を含む4つのProb01.txtファイルが必要です。
ベストアンサー1
これはどうですか…確かに大きな台無しです。
thisScript Prob01.txt 0.75 0.25
各反応に対して、最初の+/-75%変更と3番目の値+/-25%変更の組み合わせを適用し、それを別のファイルに書き込むにはそれを呼び出します。
#!/bin/bash
#takes $inputFile $pct1 $pct3
#note $pct is the multiplier expressed as a decimal
#global variables
#$w : the line number and original figures, space separated
#$newFile : the new file name
#$o : the original figures in the original format
#$n : the new figures in the correct format
inputFile=$1
#strip the suffix (.txt) from the inputFile name
outFile=${inputFile%.*}
pct1=$2
pct3=$3
function domath {
# takes $value $multiplier
local m=$(echo 1+$2 | bc -l)
local theanswer=$(echo $1 $m | awk '{printf "%7.6E\n" , $1*$2}' | sed -E -e 's/[Ee]\+*/E/g' -e 's/^([^-])/+\1/g')
echo $theanswer
}
function makechange {
#takes $reaction $case
#compose new file name
newFile=${outFile}_$1_$(printf "%02g" $2).txt
#make a copy
cp $inputFile $newFile
#change the appropriate line
sed -i "${w[0]}s/$o/$n/" $newFile
}
#get all the reaction names
grep -Po "^r[0-9]+f(?=:)" Prob01.txt > stepA
#get all the figures and their line numbers in case duplicates occur
grep -Pon "^\+[^\!]*" Prob01.txt > stepB
for ((i=1; i<=$(cat stepA | wc -l); i++)); do
reaction=$(sed "${i}q;d" stepA)
figures=$(sed "${i}q;d" stepB | sed 's/:/ /g')
w=($figures)
#retrieve the old string
o=$(echo $figures | grep -Po "(?<= ).*")
#compose the new string for each of the 4 cases
for ((j=1; j<=4; j++)); do
case $j in
1)
n=$(echo "$(domath ${w[1]} $pct1) ${w[2]} ${w[3]}")
;;
2)
n=$(echo "$(domath ${w[1]} -$pct1) ${w[2]} ${w[3]}")
;;
3)
n=$(echo "${w[1]} ${w[2]} $(domath ${w[3]} $pct3)")
;;
4)
n=$(echo "${w[1]} ${w[2]} $(domath ${w[3]} -$pct3)")
;;
esac
#make the changes
makechange $reaction $j
done
done
#clean up
rm step{A..B}