[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

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

0
totalimpact
Asked:
totalimpact
  • 4
  • 3
  • 2
1 Solution
 
DLehCommented:
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
0
 
totalimpactAuthor Commented:
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.
0
 
point_pleasantCommented:
i think you also want to print $4 on your awk statement and bash is not real good with decmil arithmitic
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
DLehCommented:
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.
0
 
DLehCommented:
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.
0
 
DLehCommented:
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
0
 
point_pleasantCommented:
this shoould work for you

# my ping script:
#!/bin/bash
TIMESTAMP="$(date +%Y%m%d%k%M)"
ping -i 2 -s 8000 -c 5 172.30.156.169 > "/var/spool/pingstats/$TIMESTAMP"
#ping -i 2 -s 8000 -c 5 172.30.156.169

#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 `ls /var/spool/pingstats/*`
do
my_var1=`grep max $i |awk '{ print $4}' |cut -d"/" -f3`
my_var=`echo "$my_var1 10" | awk '{if ($1 > $2 ) print $1;else print $2}'`
if [ $my_var != "10" ]; then
        echo "Moving $i ping value is $my_var1\n"
        my_base=`basename $i`
        mv $i /tmp/badpings/$my_base
else
        echo "No Files to move"
fi
0
 
point_pleasantCommented:
sorry use your local IP addr
0
 
totalimpactAuthor Commented:
thats what i was looking for ;)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now