Link to home
Start Free TrialLog in
Avatar of totalimpact
totalimpact

asked on

bash shell filtering of number

I am capturing ping statistics to a file, and I am trying to sort data by the max ping time to see if its higher than 10 to save that data to another folder, but I am at a loss, here is a sample of my nasty code ;)
# my ping script:
#!/bin/bash
TIMESTAMP="$(date +%Y%m%d%k%M)"
ping -i 2 -s 8000 -c 300 192.168.1.1 > "/var/spool/pingstats/$TIMESTAMP"

#sample of ping data I want to filter (2.332 is the important number):
rtt min/avg/max/mdev = 2.120/2.178/2.332/0.067 ms

# My sort script:
#!/bin/bash
for i in $(grep 'max' /var/spool/pingstats/* |awk ' { print $5}' |cut -d"/" -f3);do
if [ $i -gt 10 ]; then
mv $i /tmp/badpings/
fi
done

Open in new window

Avatar of DLeh
DLeh
Flag of United States of America image

cat "/var/spool/pingstats/$TIMESTAMP"|cut -d\/ -f 6

Open in new window


For more information about these commands, see: http://jmatrix.net/dao/case/case.jsp?case=7F000001-1A1399-10E6607463E-C29
Avatar of totalimpact
totalimpact

ASKER

I already have that portion working, I guess I am mostly having trouble with the for loop that compares the output of my grep to see if it is -gt 10.

My ping command logs to a single file for 10 minutes, then after 10 minutes it starts a new file, I can manually grep out the data  that I want - and it makes a line for each ping file, but i need to loop all that through the -gt comparison to see if the data is above 10 - this part is where i am stuck.
i think you also want to print $4 on your awk statement and bash is not real good with decmil arithmitic
This following line doesn't look right:

for i in $(grep 'max' /var/spool/pingstats/* |awk ' { print $5}' |cut -d"/" -f3);do
if [ $i -gt 10 ]; then
mv $i /tmp/badpings/

Open in new window


Do you want $i to be the line, or the timestamp name of the file.  It looks like you are trying to do both.

As written, $i should be the result output.  So for the last line, you want to do something like:

echo $i >> /tmp/badpings/somefilename

Open in new window


Since $i is not a file, but a result, you can't really "move" it.
Also, $i (if your awk is correct), will only give you a result, not the full line. So your "somefilename" will look like this:

10.234
11.872
55.982
...

And I'm not sure that's what you want.  Let me look, and I'll see if I can come up with a basic script.
ping -i 2 -s 8000 -c 300 192.168.1.1 |grep time=[1-9][0-9]

Open in new window


Results:
8008 bytes from 192.168.1.1: icmp_req=1 ttl=63 time=13.4 ms
8008 bytes from 192.168.1.1: icmp_req=2 ttl=63 time=11.4 ms

Now, all you have to do is echo that to a file via >> which you can then name by timestamp.  This will give you all times greater than or equal to 10 ms
ASKER CERTIFIED SOLUTION
Avatar of point_pleasant
point_pleasant
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry use your local IP addr
thats what i was looking for ;)