我有一个包含循环的shell脚本。这个循环正在调用另一个脚本。循环的每次运行的输出都附加在文件(outOfLoop.tr)中。当循环结束时,awk命令应计算特定列的平均值并将结果附加到另一个文件(fin.tr)。最后,打印(fin.tr)。shellscript和awk提取来计算平均值
我设法得到第一部分,将循环结果附加到(outOfLoop.tr)文件中。另外,我的awk命令似乎工作...但我没有得到格式方面的最终预期输出。我想我错过了一些东西。这里是我的尝试:
#!/bin/bash
rm outOfLoop.tr
rm fin.tr
x=1
lmax=4
while [ $x -le $lmax ]
do
calling another script >> outOfLoop.tr
x=$(($x + 1))
done
cat outOfLoop.tr
#/////////////////
#//I'm getting the above part correctly and the output is :
27 194 119 59 178
27 180 100 30 187
27 175 120 59 130
27 189 125 80 145
#////////////////////
#back again to the script
echo "noRun\t A\t B\t C\t D\t E"
echo "----------------------\n"
#// print the total number of runs from the loop
echo "$lmax\t">>fin.tr
#// extract the first column from the output which is 27
awk '{print $1}' outOfLoop.tr >>fin.tr
echo "\t">>fin.tr
#Sum the column---calculate average
awk '{s+=$5;max+=0.5}END{print s/max}' outOfLoop.tr >>fin.tr
echo "\t">>fin.tr
awk '{s+=$4;max+=0.5}END{print s/max}' outOfLoop.tr >>fin.tr
echo "\t">>fin.tr
awk '{s+=$3;max+=0.5}END{print s/max}' outOfLoop.tr >>fin.tr
echo "\t">>fin.tr
awk '{s+=$2;max+=0.5}END{print s/max}' outOfLoop.tr >> fin.tr
echo "-------------------------------------------\n"
cat fin.tr
rm outOfLoop.tr
我想要的格式是这样的:
noRun A B C D E
----------------------------------------------------------
4 27 average average average average
我已经0.5递增max
awk命令里面,因为是出把结果之间的新生产线( outOfLoop文件的输出)
你为什么不在[tag:awk]中做所有事情? – Johnsyweb 2013-05-10 03:39:15
我的问题不是来自shell脚本。这是从awk格式化的!!如果我在awk中完成整个...问题仍然没有解决! – SimpleNEasy 2013-05-10 03:53:13
在awk中编写整个东西并不能保证解决你的问题,但是如果你有这个问题,它会让你不太可能首先遇到问题,而且更容易解决问题。 – 2013-05-10 04:00:15