Computing.Net > Forums > Unix > Loop Script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Loop Script

Reply to Message Icon

Name: Hugo
Date: May 9, 2008 at 08:26:19 Pacific
OS: Sol 9
CPU/Ram: 4GB
Product: v240
Comment:

Folks,

I have a script that ping 40 sites every 5 minutes. The output of it is in this format:

Mylocation,Destination City - Country,transport,(IP Address):,mm-dd-yy hh:mm,avg latency last 2 pingresults, i.e:

Edison,Hong Kong - China,MPLS,(10.10.22.1):,05-09-08 09:00,318
...
...
Edison,location 40, etc.etc.

and repeats again with results of 40 sites for last five minutes.
Edison,Hong Kong - China,MPLS,(10.10.22.1):,05-09-08 09:05,320

The task...

I need to average the averages for each destination every hour, and every day.

I put this and it works, but how do I do it more effinciently without having to create many interim files and several cron jobs? I was thinking of a loop greping for every location, evaluating average is not empty, so when dividing by total entries for each destination result is accurate.

I am having problem writing a loop script.


#!/bin/ksh

cd /tmp
grep destination1 /tmp/pingresults.txt > dest1.txt
nawk -F, '$6 >=1 {print $6 }' dest1.txt | TR=`wc -l`
nawk -F, '$6 >=1 { x=x+$6 } END { print "Avg = " (x/'"$TR"') }' dest1.txt > avgdest1
grep destination2 /tmp/pingresults.txt > dest2.txt
nawk -F, '$6 >=1 {print $6 }' dest2.txt | TR=`wc -l`
nawk -F, '$6 >=1 { x=x+$6 } END { print "Avg = " (x/'"$TR"') }' dest2.txt > avgdest2
...
and so on for other 38 destinations

I am looking to have one entry for each destination with average for last hour, and last 24 hours, it can be in two different files, using same format as original output.

Your suggestions and ideas will be greatly appreciated.

Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: Devaraj (by Fidy)
Date: May 11, 2008 at 22:15:05 Pacific
Reply:

You may want to try something like this:

awk 'BEGIN{x1=0;count1=0;x2=0;count2=0}$0 ~ /destination1/ { if ($6 >= 1){x1+=$6;count1+=1}
$0 ~ /destination2/{if ($6 >= 1){x2+=$6;count2+=1}
END{print "Avg for des1 is:,$x1/$count1,"Avg for dest2 :", $x2/$count2}' /tmp/pingresults.txt


cheers,
Devaraj Takhellambam

Regards,
Big Gun


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Loop Script

Bourne shell script,Unix www.computing.net/answers/unix/bourne-shell-scriptunix/5366.html

loops in shell scripts!!! www.computing.net/answers/unix/loops-in-shell-scripts/5140.html

write log in loop - ksh script www.computing.net/answers/unix/write-log-in-loop-ksh-script/4721.html