Operating System - HP-UX
1753782 Members
7360 Online
108799 Solutions
New Discussion юеВ

sums averages in using expr, awk

 
SOLVED
Go to solution
rmueller58
Valued Contributor

sums averages in using expr, awk

all,

I am using vmstat and sar, piping output to flat files for some long term performance monitoring.
I want want to create a script that builds a running average of CPU idle, and memory use.
I will have columnar data, I want to summarize daily data files, with sums and averages using "expr", and "awk".

Basically from vmstat out put I will keep a timestamp, avg, free.

I will do something similar with "sar", capturing CPU1 idle, CPU2 idle, and timestamp.


Basically,

I want to create a running average, and a daily average

if Free is Column 2 I want to sum the column and divide by "wc -l"

If someone can show me a quicky on how to use an expr to sum my columns I can probably figure the rest out.





6 REPLIES 6
RAC_1
Honored Contributor

Re: sums averages in using expr, awk

awk -F "sepeator" '{s+=$1/$whatever} END {print s/NR}'

Anil
There is no substitute to HARDWORK
Rodney Hills
Honored Contributor

Re: sums averages in using expr, awk

A typical way I have summed a column in awk is-

awk 'BEGIN{s=0;n=0};{n++;s=s+$2};END{print $s/$n}' myinputfile

HTH

-- Rod Hills
There be dragons...
Mark Greene_1
Honored Contributor

Re: sums averages in using expr, awk

You can do this in ksh and use the built-in to do the math, as it is much more straight-forward I think:

(( TOTAL_CPU1 = $TOTAL_CPU1 + SAR_CPU1 ))

where you can use some looping mechanism of your choice (for, while, etc) to loop through the file or variable where you've captured the sar data to be totaled.

mark
the future will be a lot like now, only later
rmueller58
Valued Contributor

Re: sums averages in using expr, awk

Rod,

in the awk
'BEGIN{s=0;n=0};{n++;s=s+$2};END{print $s/$n}'

is the $2 the column of which I want to sum?
Rodney Hills
Honored Contributor
Solution

Re: sums averages in using expr, awk

Yes $2 is the second field.

Change $s/$n to s/n (sometimes I think perl :-)).

-- Rod Hills
There be dragons...
rmueller58
Valued Contributor

Re: sums averages in using expr, awk

Thanks All.

Did it!!