How to write such kind of script?

How to write such kind of script?

Post by yezi » Wed, 07 Jun 2006 05:23:48


Dear all:

I try to code script to realize function which is described as after:

I have tons of fles which is end with *.ping, try to parse out 2
parameters from the last line of each file. The sentence is like as
following :

rtt min/avg/max/mdev = 136.200/136.643/137.152/0.612 ms, pipe 2

The two paremeters are :

1> avg , which is 136.643
2> mdev, which is 0.612

Thanks for any hint.

bin
 
 
 

How to write such kind of script?

Post by Bit Twiste » Wed, 07 Jun 2006 05:45:11


#***************************************************
# just a hint then

_ifs_bkup=$IFS

line="rtt min/avg/max/mdev = 136.200/136.643/137.152/0.612 ms, pipe 2"

IFS=" /"
set -- $(echo $line)
shift 6
echo avg=$2 mdev=$4
IFS=$_ifs_bkup

#***************************************************


For extra points
! bash script advanced documentation
http://www.yqcomputer.com/
! bash script introduction to Linux documentation
http://www.yqcomputer.com/

 
 
 

How to write such kind of script?

Post by Logan Sha » Wed, 07 Jun 2006 14:27:12


To get only the lines you care about:

grep 'min/avg/max/dev' *.ping

To parse a value from each line, pipe the line into awk to get just
the series of numbers:

awk '{print $4}'

Then use cut to get only the one you care about:

cut -d/ -f2 (for avg)
cut -d/ -f4 (for mdev)

For example, to get the avg from all the files:

grep 'min/avg/max/dev' *.ping | awk '{print $4}' | cut -d/ -f2

Hope that helps.

- Logan