我正在编写一个小程序,其功能如下。它使用名为hist1.dat的文件,第一步就是这个
awk '{if(NR>1) s+=$2*($1-x); x=$1}END{print s}' hist1.dat >int1.txt
hist1.dat如下所示
0.259990113102 4
0.261752339307 10
0.263514565512 15
0.265276791717 35
0.267039017922 58
0.268801244127 84
0.270563470333 147
0.272325696537 217
0.274087922742 316
0.275850148947 410
0.277612375152 583
0.279374601357 750
0.281136827562 881
0.282899053767 1004
0.284661279972 1241
int1.txt
包含值79.9504
。第二步使用此值并进一步处理,如下面的
awk '{if(NR>1) s+=$2*($1-x); print $1,s/c1; x=$1}' c1="$(cat int1.txt)" hist1.dat >cutoff1_org.txt
cutoff1_org.txt
如下所示
0.321668030277 0.832661
0.323430256483 0.851265
0.325192482688 0.867267
0.326954708893 0.882167
0.328716935097 0.895634
0.330479161302 0.906677
0.332241387507 0.917587
0.334003613712 0.927175
0.335765839917 0.935287
0.337528066122 0.9432
0.339290292327 0.94968
0.341052518532 0.955675
0.342814744737 0.961097
0.344576970942 0.96555
0.346339197147 0.969231
如果第2列中的相应值接近0.95,则下一个脚本使用cutoff1_org.txt文件并尝试在第1列中查找值。这也很有效
awk -v c=2 -v t=0.95 'NR==1{d=$c-t;d=d<0?-d:d;v=$c;next}{m=$c-t;m=m<0?-m:m}m<d{d=m;v=$1}END{print v}' cutoff1_org.txt >final_cutoff1.txt
接下来的两个脚本将只使用final_cutoff1.txt中的值。像下面这样的东西
awk '{ if ($1 >= cutoff1) print $1 }' cutoff1="$(cat final_cutoff1.txt)" hist1.dat >hist_oc1.dat
awk '{ if ($1 <= cutoff1) print $1 }' cutoff1="$(cat final_cutoff1.txt)" hist1.dat >hist_uc1.dat
现在我想把一个循环放在一个循环中,如下面的
for i in {1..22}; do
awk '{if(NR>1) s+=$2*($1-x); x=$1}END{print s}' hist${i}.dat >int${i}.txt
awk '{if(NR>1) s+=$2*($1-x); print $1,s/c${i}; x=$1}' c${i}="$(cat int${i}.txt)" hist${i}.dat >cutoff${i}_org.txt
awk -v c=2 -v t=0.95 'NR==1{d=$c-t;d=d<0?-d:d;v=$c;next}{m=$c-t;m=m<0?-m:m}m<d{d=m;v=$1}END{print v}' cutoff${i}_org.txt >final_cutoff${i}.txt
awk '{ if ($1 >= cutoff${i}) print $1 }' cutoff${i}="$(cat final_cutoff${i}.txt)" hist${i}.dat >hist_oc${i}.dat
awk '{ if ($1 <= cutoff${i}) print $1 }' cutoff${i}="$(cat final_cutoff${i}.txt)" hist${i}.dat >hist_uc${i}.dat
cat hist_oc${i}.dat |stdev >stat_oc${i}.txt
cat hist_uc${i}.dat |stdev >stat_uc${i}.txt
done
但是我收到了如下错误
的错误awk: cmd. line:1: {if(NR>1) s+=$2*($1-x); print $1,s/c${i}; x=$1}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: {if(NR>1) s+=$2*($1-x); print $1,s/c${i}; x=$1}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: { if ($1 >= cutoff${i}) print $1 }
awk: cmd. line:1: ^ syntax error
我想我现在已经褪色了。似乎很容易修复错误。任何人都可以指出并帮助它。非常感谢提前