Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Networking
User Name
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.


  Search this Thread
Old 02-12-2004, 03:57 AM   #1
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 then it picks the wrong section.

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

Rep: Reputation: 0
how about this:

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
Senior Member
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
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.

echo ""
echo "Enter hostname or ip address: "
read host
ping -c 3 ${host} | grep --max-count=1 'rtt' |cut -d/ -f5 | cut -d/ -f4 >temp.txt
cat ${ip} | \
while read avg
echo " average is $avg"
Old 02-12-2004, 09:54 AM   #4
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.

#!/bin/env python

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

tempfile = "/tmp/pingfile"
logfile = "/var/log/pingrate.log"
ipaddr =""
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))


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 14 08-09-2016 09:49 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 > Forums > Linux Forums > Linux - Networking

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

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