"how do I extract a number from a text file using shell command?"
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
"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?
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.
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
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))
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.