Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Subject: nawk or sed help

Original Message
Name: Hugo
Date: April 30, 2008 at 13:57:44 Pacific
Subject: nawk or sed help
OS: Sol 9
CPU/Ram: 4GB
Model/Manufacturer: v240
Comment:
Hi,
I've written a one line script to consolidate ping stats for each device being pinged from a list of 30 devices. I am using a for-do loop and extracting and formatting the values. But I'm having problems extracting just the average value:

My output so far looks like this:

MyCity to device.domain.com (10.10.10.12): 04-30-08 13:05:00 min/avg/max = 103/106/113

I need it to show only the average value, like this:
MyCity to device.domain.com (10.10.10.12): 04-30-08 13:05:00 avg = 106

How can I accomplist this since the value average is not always 3 characters? I tried using nawk and sed, but no luck yet extracting just the average for each ping result.

Thanks


Report Offensive Message For Removal

Response Number 1
Name: nails
Date: April 30, 2008 at 15:14:03 Pacific
Subject: nawk or sed help
Reply: (edit)
Consider this:

echo "MyCity to device.domain.com (10.10.10.12): 04-30-08 13:05:00 min/avg/max = 103/106/113" | nawk
' BEGIN { FS="/" } { $3="="
$5=""
gsub("min","")
print $0} '

It's a kludge and it's easily broken if the data structure varies from what you've said.

Using a field seperator, FS, equal "/", then
field 3 should be replaced by an equal sign and field 5 should be nulled out. That leaves the "min" string which is removed by a call to gsub.


Report Offensive Follow Up For Removal

Response Number 2
Name: ghostdog
Date: April 30, 2008 at 20:40:29 Pacific
Subject: nawk or sed help
Reply: (edit)

awk '{
n=split($NF,a,"/")
for(i=1;i<=n;i++) total+=a[i]
$NF = total/n
print
}
' output


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: May 1, 2008 at 19:50:03 Pacific
Subject: nawk or sed help
Reply: (edit)
ghostdog:

I executed your awk script and this is the output I received:


MyCity to device.domain.com (10.10.10.12): 04-30-08 13:05:00 min/avg/max = 107.333

The requirement is for the end of the string should look like this:

... avg = 106


Report Offensive Follow Up For Removal

Response Number 4
Name: ghostdog
Date: May 1, 2008 at 21:18:14 Pacific
Subject: nawk or sed help
Reply: (edit)
nails:

maybe i misinterpreted what OP wants. I thought he meant to take the average of the 3 numbers. So its the middle number he wants..therefore, the for loop should be removed, and a[2] will be the average nnumber... thanks for pointing out


Report Offensive Follow Up For Removal

Response Number 5
Name: Hugo
Date: May 2, 2008 at 06:53:28 Pacific
Subject: nawk or sed help
Reply: (edit)
Thanks all for the suggestions. Not the cleanest, but it did the job for me:
#!/bin/ksh

DATE=`date '+%m-%d-%y %H:%M'`

for devices in `cat /tmp/devices` do
/usr/sbin/ping -ns $devices 56 2 | sed -e '2,5d' -e 's/PING /From City to/' -e 's/56 data bytes//' -n -e '/(/p' | sed -e :a -e '$!N;s/\nround-trip (ms) min\/avg\/max =/'"$DATE"'/' |
nawk -F/ '{print $1, $2}' | nawk '{print $1"," $2"," $3"," $4"," $5"," $6}' >> /tmp/results


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: nawk or sed help

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software



Version Tracker Pro
Keep your software current and secure, effortlessly

Click Here for a Free Scan

Driver Agent
Automatically find the latest drivers for your computer.
Click Here for a Free Scan



The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC