shell - shellscript and awk extraction to calculate averages -
i have shell script contains loop. loop calling script. output of each run of loop appended inside file (outofloop.tr). when loop finished, awk command should calculate average of specific columns , append results file(fin.tr). @ end, (fin.tr) printed.
i managed first part appending results loop (outofloop.tr) file. also, awk commands seem work... i'm not getting final expected output in terms of format. think i'm missing something. here try:
#!/bin/bash rm outofloop.tr rm fin.tr x=1 lmax=4 while [ $x -le $lmax ] calling script >> outofloop.tr x=$(( $x + 1 )) done cat outofloop.tr #///////////////// #//i'm getting above part correctly , output : 27 194 119 59 178 27 180 100 30 187 27 175 120 59 130 27 189 125 80 145 #//////////////////// #back again script echo "norun\t a\t b\t c\t d\t e" echo "----------------------\n" #// print total number of runs loop echo "$lmax\t">>fin.tr #// extract first column output 27 awk '{print $1}' outofloop.tr >>fin.tr echo "\t">>fin.tr #sum 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 format :
norun b c d e ---------------------------------------------------------- 4 27 average average average average
i have incremented max
inside awk command 0.5 there new line between out put of results (output of outofloop file)
$ 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 { (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 b c d e ---------------------------------------------------------- 4 27 184.5 116 57 160
Comments
Post a Comment