To find sum & average of 8th field

Hi Friends,

I have many files like below.

total,0.7%,0.0%,0.2%,0.0%,0.2%,0.7%,98.0%
total,1.9%,0.0%,0.4%,0.0%,0.0%,6.8%,90.6%
total,0.9%,0.0%,0.4%,0.0%,0.0%,0.0%,98.5%
total,1.4%,0.0%,0.7%,0.0%,0.2%,2.9%,94.5%
total,0.7%,0.0%,0.4%,0.0%,0.0%,0.9%,97.7%
total,0.9%,0.0%,0.4%,0.0%,0.0%,2.9%,95.5%
total,1.4%,0.0%,0.0%,0.0%,0.0%,0.0%,98.5%
total,1.2%,0.0%,0.4%,0.0%,0.0%,0.0%,98.2%
total,0.9%,0.0%,0.9%,0.0%,0.0%,1.2%,96.8%
total,2.7%,0.0%,0.4%,0.0%,0.0%,0.0%,96.7%
total,1.4%,0.0%,0.4%,0.0%,0.4%,0.7%,96.7%

There are 8 fields in above file separated by a comma. I want to find out max & average of 8th field.

Thanks,
Sunil

awk -F, 'BEGIN{max=0;total=0}{if($8 > max){max =$8};total=total+$8}END{print max,total/NR}' text.txt
1 Like

Try:

awk -F, '{sub ("%","",$8);sum+=$8;if ($8>max){max=$8}}END{printf "max=%.1f\%\navg=%.1f\%\n",max,sum/NR}' file
1 Like

Another approach:

awk -F"[,%]" '{max=$(NF-1)>max?$(NF-1):max; som+=$(NF-1)}END{print max, som/NR}' file
1 Like

long but effective and easy approach!!

a=`cat filename |awk -F"," '{print $8}' |tr "%" " " |sort |tail -1`
b=`cat filename |awk -F"," '{print $8}' |tr "%" " " |awk '{c +=$1}END{print c}'`
e=`cat filename |awk -F"," '{print $8}'|wc -l `
d=`echo "$b $e" |awk '{print $1/$2}'`
echo " Max value is $a"
echo " Sum is $b"
echo " Average is $d"

:stuck_out_tongue:

1 Like

Thanks a lot. The solution suggested is working fine.

---------- Post updated at 04:04 PM ---------- Previous update was at 04:03 PM ----------

Thanks a lot. The solution suggested is also working fine.

---------- Post updated at 04:05 PM ---------- Previous update was at 04:04 PM ----------

Thanks a lot. The solution suggested is also working fine.

---------- Post updated at 04:11 PM ---------- Previous update was at 04:05 PM ----------

Thanks a lot. This solution is also working.

---------- Post updated at 04:19 PM ---------- Previous update was at 04:11 PM ----------

Hi Friends,

All the solutions provided are working fine.

There is a slight change in requirement from my customer.

Actually the value of 8th field is the CPU Ideal time. The CPU usage time will be 100-(CPU Ideal time) i.e. 100-(8th Field value).

So i want your help to calculate a new field (100 - 8th field value) & find the max & average of this new field.

Thanks a lot.

Sunil

awk -F, '{sub ("%","",$8);sum+=100-$8;if (100-$8>max){max=100-$8}}END{printf "max=%.1f%\navg=%.1f%\n",max,sum/NR}' file
1 Like
awk -F, 'BEGIN{max=0;total=0}{if((100-$8) > max){max =(100-$8)};total=total+(100-$8)}END{print max,total/NR}' text.txt
1 Like

Thanks. This solution is working fine.

---------- Post updated at 05:03 PM ---------- Previous update was at 05:02 PM ----------

Thanks. This solution is also working fine.

Hi,

How can I run this command on multiple files at a time?

Thanks,
Sunil

Give multiple file names seperated by space instead of just 'file'

1 Like
perl -F, -lane 'BEGIN{$max=0;$avg=0;$i} $i++;$F[7]=~s/%//g; $max=$F[7] if($F[7]>$max); $avg+= $F[7];END{print $max;print($avg/$i)}'

Thanks,
MG

1 Like
# awk -F"[,%]" '{x=$(NF-1);if(m<x)m=x;s+=x}END{printf "max=%.1f%%\nsum=%.1f\navg=%.1f%%\n",m,s,s/NR}' infile
max=98.5%
sum=1061.7
avg=96.5%

Use nawk or /usr/xpg4/bin/awk if running on SunOS/Solaris plateform

Note that the display of the '%' as litteral may vary from OS and/or OS version of awk/nawk... implementation to another.
(some must be written \% some other %% and some tolerate a simple %)...so ... just adapt the code so it displays the % correctly for you.

1 Like

Thank you all for your help. My issue is resolved.

Best Regards,
Sunil