awk script to count percentage from log file

Hi,

I have a log like this :

actually i want to get the log like this :

where % can get from :

100 * pmTotNoRrcConnectReqSucc / pmTotNoRrcConnectReq

Thanks in advance.. :slight_smile:

Can you calc the % for one of them? What is pmTotNoRrcConnectReqSucc and pmTotNoRrcConnectReq for the object 30011 ?

oh yea.. the value for object 30011 for example:

so it will be :

btw can we do it just 1 command line?

if you have Python, here's an alternative solution

#!/usr/bin/env python
from __future__ import division
d={}
f=open("file")
header=f.readline()
for line in f:        
    line=line.strip().split()
    d.setdefault(line[0],[])
    d[line[0]].append(sum(map(int,line[2:6])))
for i,j in d.iteritems():
    try:
        print i,100*j[1]/j[0]        
    except Exception,e: 
        print e

output:

# ./test.py
30033 99.2307692308
30032 99.3808049536
30031 99.8797354179
30011 100.347222222
30013 100.0
30012 99.7777777778
30051 99.7604790419
30053 99.7389033943
30052 99.6088657106
30101 98.8571428571
30061 99.8313659359
30062 99.9547920434
30063 99.605781866
30082 float division
30083 float division
30081 99.7353797301
30042 99.5249406176
30043 99.1323210412
30041 100.0
30021 99.8188405797
30022 100.355871886
30023 100.0
30103 101.231527094
30073 99.3121693122
30072 100.0
30071 98.6825595985
30091 99.8564249821
30102 99.4520547945
30093 99.8550724638
30092 99.9460334593

awk '$0 !~ /Object/{fl=$3+$4+$5+$6;getline;sc=$3+$4+$5+$6;fl=(fl==0)?1:fl;print $1,100*(sc/fl);next}' file

asuuming succ=0 and fl=0 will return 0

cheers,
Devaraj Takhellambam

nawk '{
if (NR==1)
next
else
for(i=3;i<=6;i++)
_[$1]+=$i
}
END{
print "OBJECT PERCENTAGE"
for(i in _)
	print i" "_/4
}' a.txt

oke thanks all. :slight_smile:
btw for ghostdog74 and devtakh, how to alow just only 2 digit after comma ?

What exactly do you mean. 2 digit after the precision for the floating % value?

awk '$0 !~ /Object/{fl=$3+$4+$5+$6;getline;sc=$3+$4+$5+$6;fl=(fl==0)?1:fl;printf("%s %.2f%\n", $1,100*(sc/fl));next}' file

o/p
30011 100.35%

cheers,
Devaraj Takhellambam

yes devtakh.. thanks for ur help

regards,