Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


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

Rep: Reputation: 15
How to pick a string from a text file?

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, 04:17 AM   #2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391
As you already noticed, you are grabbing a fixed amount of positions.

There are many ways to solve this, here's (part) of one that uses cut.

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

The bold part is what you want to see. You can tell cut to use a different seperator (a spcae/tab is standard), which comes in handy, because the / could be used for this:

cut -d"/" -f7

Which does the following:

-d"/" => set seperator to /
-f7 => show field number 7

What you are left with is 14.284 ms, not the way you want it yet, but the rest you can try yourself

Hope this gets you going again.


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace a string in a text file jpan Linux - General 3 10-14-2012 06:17 PM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
how to modify a text file or a string in shell programming luckyvietman Programming 5 07-12-2005 05:08 PM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM
Parsing a file for a string of text jamesmwlv Linux - General 2 12-02-2002 07:13 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:19 PM.

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