LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices



Reply
 
Search this Thread
Old 02-12-2004, 03:57 AM   #1
sdandeker
LQ Newbie
 
Registered: Sep 2003
Location: UK
Distribution: Gentoo
Posts: 22

Rep: Reputation: 15
Talking "how do I extract a number from a text file using shell command?"


Hello shell script programmers!
Im new to all this so go easy on the explanations please.

I want to use Shell commands only (Not perl) to ping a destination and then assign the average value to a variable.

Ive managed to do it using a combination of 'grep' and 'cut' but I run into problems when the result is more than 100.000 ms or less than 10.000ms since the number of digits is different. Make sense?

So I have:

ping -c 3 "$ip">temp1
grep "rtt" temp1 | cut -b31-36>temp2
read average<ukbabi1u2

This assigns the latency to $average. The cut command simply picks the average value from the line:

rtt min/avg/max/mdev = 63.385/78.393/97.608/14.284 ms

BUT if the results are not xx.xxx then it picks the wrong section.

Any better solutions?
 
Old 02-12-2004, 05:20 AM   #2
dennisa
LQ Newbie
 
Registered: May 2003
Location: Sweden
Distribution: Debian
Posts: 3

Rep: Reputation: 0
how about this:

Code:
average=`ping -c 3 "$ip" | grep rtt | cut -d = -f 2 | cut -d / -f 2`
it will ping the other box, grep the line with the numbers, get the numbers to the right of the equal sign, get the second number in the list and put the result in the variable named average.

/ Dennis
 
Old 02-12-2004, 09:41 AM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Using cut , you can count the fields ( f5 and f4 ) separated by the delimitator ( -d/ ) so if you have
rtt min/avg/max/mdev = 63.385/78.393/97.608/14.284 ms
cut -d/ -f5 | cut -d/ -f4 should get you to the right place.

Code:
#!/bin/bash
echo
echo ""
echo "Enter hostname or ip address: "
read host
echo
ping -c 3 ${host} | grep --max-count=1 'rtt' |cut -d/ -f5 | cut -d/ -f4 >temp.txt
ip="temp.txt"
cat ${ip} | \
while read avg
do
echo " average is $avg"
echo
echo
done
 
Old 02-12-2004, 09:54 AM   #4
jazernorth
Member
 
Registered: Jan 2004
Location: Green Bay
Distribution: RedHat 8.0, LFS-5.0
Posts: 100

Rep: Reputation: 15
This might be a little overboard, but this is what I use to ping my ISP every 10 minutes. We had an argument over connection uptime, and I won. I had proof that their connection was not up. I had to use this to do it. So, here is what I used to as a cron job.

Someone else might be able to clean it up for you, but it does the job.

Code:
#!/bin/env python

import os, string                               # This imports String and system functions
import time

tempfile = "/tmp/pingfile"
logfile = "/var/log/pingrate.log"
ipaddr ="xxx.xxx.xxx.xxx"
ptime = "50"

date1 = time.localtime()
date2 = string.split(str(date1), ',')
datecent = date2[0][1:5]

for i in range (1,6) :
    date2[i] = string.replace(date2[i], ' ', '0')
    date2[i] = string.replace(date2[i], ' ', '0')
    date2[i] = string.replace(date2[i], ' ', '0')

seconds = date2[5][1:3]
minutes = date2[4][1:3]
hours = date2[3][1:3]

if (date2[5][2:3] == '') :
    seconds = date2[5][0:2]

if (date2[4][2:3] == '') :
    minutes = date2[4][0:2]

if (date2[3][2:3] == '') :
    hours = date2[3][0:2]
fulldate = "%s/%s/%s" % (date2[1], date2[2][1:3], datecent)
fulltime = "%s:%s:%s" % (hours, minutes, seconds)

os.system ("ping %s -w %s | grep tt | grep -v ttl > %s" % (ipaddr, ptime, tempfile)) # This does the ping. I can't deal with the outpute directly. So,
                # instead I write it to a file

exist = os.path.isfile ('%s' % (tempfile))
if (exist) :                                           # See the file was created correctly
        file = open( tempfile, 'r' )
        recs = file.readlines()                 # stores the file into the varialbe recs
        length = len(recs)                      # gets the number of lines in recs (the file)

        if (length > 1) :                           # If there are at least 2 lines
            a = recs[length - 2]                # Sets a to the 2nd to last line
            b = recs[length - 1]

            b2 = string.split(b,'/')            # Does same thing to b, but uses '/' as the delimiter
            Ave = b2[4]
            Max = b2[5]
            a2 = string.split(a, ' ')           # Makes an array, a2, where the elements are the portions of 'a' between ' '
            Sent = a2[0]                                # Gets the first ele of a2 (number of packets sent)
            Received = a2[3]
            Percent = a2[5]
            Lost = int(Sent) - int(Received)    # Computes the number of packets lost. Sent and Received are strings, so they \
            RTime = string.split(a2[8], 'm')

            message = "%s, %s, ip=%s, Sent=%s, Lost=%s, Per=%s, Avg=%s, Max=%s ms, RTime=%s" % \
                (fulldate, fulltime, ipaddr, Sent, Lost, Percent, Ave, Max, RTime[0])

            #   print message
            os.system ("echo %s >> %s" % (message, logfile))    # prints out the message.  You can just redirect
                                                                # this wherever you want
        if (length < 2) :
            os.system ("echo %s, %s, ip=%s, FAILED 100 PERCENT >> %s" % (fulldate, fulltime, ipaddr, logfile))
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to extract a "tar.bz2" file? deWin Linux - Newbie 11 11-05-2009 12:55 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
Unable to extract "tar.gz" file - please try for me Riddick Linux - Software 6 08-27-2005 05:57 PM
Inputting a text file in to the "rm" command R4z0r Linux - General 5 12-28-2003 06:44 PM
How to extract (unpack) file in "A" driver matin Linux - Newbie 1 03-01-2002 07:59 AM


All times are GMT -5. The time now is 08:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration