简体   繁体   English

shellscript和awk提取以计算平均值

[英]shellscript and awk extraction to calculate averages

I have a shell script that contains a loop. 我有一个包含循环的shell脚本。 This loop is calling another script. 此循环正在调用另一个脚本。 The output of each run of the loop is appended inside a file (outOfLoop.tr). 循环的每次运行的输出都将附加到文件(outOfLoop.tr)中。 when the loop is finished, awk command should calculate the average of specific columns and append the results to another file(fin.tr). 循环结束后,awk命令应计算特定列的平均值,并将结果附加到另一个文件(fin.tr)。 At the end, the (fin.tr) is printed. 最后,将打印(fin.tr)。

I managed to get the first part which is appending the results from the loop into (outOfLoop.tr) file. 我设法得到了第一部分,它将循环的结果附加到(outOfLoop.tr)文件中。 also, my awk commands seem to work... But I'm not getting the final expected output in terms of format. 同样,我的awk命令似乎也可以工作...但是我没有得到格式方面的最终预期输出。 I think I'm missing something. 我想我缺少了一些东西。 Here is my try: 这是我的尝试:

#!/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

I want the format to be like : 我希望格式如下:

noRun    A       B           C            D         E
----------------------------------------------------------
4        27      average    average      average   average

I have incremented max inside the awk command by 0.5 as there was new line between the out put of the results (output of outOfLoop file) 我将awk命令中的max增加了0.5,因为结果的输出(outOfLoop文件的输出)之间有新行

$ cat file
27 194 119 59 178

27 180 100 30 187

27 175 120 59 130

27 189 125 80 145

$ cat tst.awk
NF {
    for (i=1;i<=NF;i++) {
        sum[i] += $i
    }
    noRun++
}
END {
    fmt="%-10s%-10s%-10s%-10s%-10s%-10s\n"
    printf fmt,"noRun","A","B","C","D","E"
    printf "----------------------------------------------------------\n"
    printf fmt,noRun,$1,sum[2]/noRun,sum[3]/noRun,sum[4]/noRun,sum[5]/noRun
}

$ awk -f tst.awk file
noRun     A         B         C         D         E
----------------------------------------------------------
4         27        184.5     116       57        160

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM