我有一個包含循環的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