cancel
Showing results for 
Search instead for 
Did you mean: 

scripting help

SOLVED
Go to solution
Vito Sarducci
Regular Advisor

scripting help

I want to pull the last line out from a the sar report im running on a daily basisin cron. I want to put this data to another file and concatenate each day for thirty days.

I want to take the average line and totals and add each days average line to this file so that I have approximately a file with 30 days of my sar data in it?

I have a cron job that produces this report each day. The output file it creates each day is called hostname_monthday. example would be:

system1_1029 for yesterdays output of this report.

21:45:00 3 3 2 92
22:00:01 3 3 2 92
22:15:00 9 7 53 32
22:30:00 3 4 91 1
22:45:00 3 5 91 1
23:00:00 3 5 91 1
23:15:00 3 4 91 1
23:30:00 3 4 91 1
23:45:00 3 4 90 2

Average 21 6 24 50

How can I accomplish this?

Please advise?

Vito
Lifes too short to stress out, Enjoy every day you have on earth!
8 REPLIES
Rodney Hills
Honored Contributor

Re: scripting help

how about

tail -1 hostname_month >>collect_file

tail -1 gets the last line and >> will append it to collect_file

-- Rod Hills
There be dragons...
Santosh Nair_1
Honored Contributor

Re: scripting help

How about something like this:
LOG=$(date +%m).Summary
echo "$DATE
\c ">> $LOG
sar -f |grep "Average" >> $LOG

This would put a date and the average line from sar into a file called $LOG, i.e. monthy.Summary. Just have to define the DATE variable.

-Santosh
Life is what's happening while you're busy making other plans
Craig Rants
Honored Contributor

Re: scripting help

Vito,

Based on your output here is how you would get the last line from your file

cat FILENAME | awk '{ print $5 }' > OUTPUT

To use mathmatical formulas look at the expr command

A simple example would be:

AVG=`expr TOTAL/30`

These should get you started down the right road.
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Sachin Patel
Honored Contributor

Re: scripting help

Hi Vito,
If I understand properly you need last line only. easy
#tail -1 filename >> outputfile

But I think you that is not what you want because it is seems too easy.

Sachin
Is photography a hobby or another way to spend $
Craig Rants
Honored Contributor

Re: scripting help

oops, didn't read that well. The awk statment is for the last column, not the last line.

"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Vito Sarducci
Regular Advisor

Re: scripting help

Yes, all these examples are great, but were missing one step. I need to take the data from 30 of these files per month and tail them all, which im not exact on how to do this?

I have 30 of the system1_1029, system1_1028,system1_1027 yada, yada, down to system1_1001...

So if i tail all of them, how do i get all the average lines into one output file?

Please advise?

Vito
Lifes too short to stress out, Enjoy every day you have on earth!
Rodney Hills
Honored Contributor

Re: scripting help

If you want to process on multiple files, then use "for"

for hostfile in system1* ; do
last -1 $hostfile >>collect_file
done

This will extract the last line of each of the files and append onto collect_file

-- Rod Hills
There be dragons...
Alan Riggs
Honored Contributor
Solution

Re: scripting help

for i in system1_10*
do
tail -1 $i
done |awk '{T2+=$2} {T3+=$3} {T4+=$4} {T5+=$5} {print} END {printf"AVE: %f %f %f\n",T2/NR,T3/NR,T4/NR,T5/NR}' >> 10.sum



Play with formatting etc. as you desire.