LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2011, 10:14 AM   #1
xunling
LQ Newbie
 
Registered: Sep 2008
Posts: 6

Rep: Reputation: 0
hi .. i need a shell function to index a string ....


hi...

can somebody help me ...

if i echo ping -c1 www.tiberiansun.com i get

PING www.tiberiansun.com \(209.85.147.104\) 56\(84\) bytes of data.--- google.de ping statistics ---1 packets transmitted, 0 recieved, 100% packet loss, time 13ms

i want to extract the part of time 13ms after the string ....

if i use

echo `expr index "$str1" "time"`

i get always 11 ...

can somebody help me plz?
the entire script?
 
Old 08-16-2011, 10:27 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by xunling View Post
hi...can somebody help me ...

if i echo ping -c1 www.tiberiansun.com i get

PING www.tiberiansun.com \(209.85.147.104\) 56\(84\) bytes of data.--- google.de ping statistics ---1 packets transmitted, 0 recieved, 100% packet loss, time 13ms

i want to extract the part of time 13ms after the string ....

if i use
echo `expr index "$str1" "time"`
i get always 11 ...

can somebody help me plz?
the entire script?
We will HELP you, but no, we are NOT going to write the entire script for you. And spell out your words.

Post what you've written so far, because just seeing that one line with no context above it doesn't indicate why it's not working, since we don't know what "str1" is being set and how. If I had to write the function, I'd pipe the ping output into grep for "time=", then use awk and split on the "=" character, and get the time from that.

Last edited by TB0ne; 08-16-2011 at 10:28 AM.
 
Old 08-16-2011, 10:52 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
From the expr man page:
Code:
index STRING CHARS
    index in STRING where any CHARS is found, or 0
So it is behaving as expected; the first letter of "time" in the ping output is the t of tiberiansun.
Try using expr substr instead of expr index or bash parameter expansion:
Code:
time=${str1##*time }
 
1 members found this post helpful.
Old 08-16-2011, 01:07 PM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-17-2011, 01:21 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am curious why you would echo the output of the ping command seeing it will already display its own output?
 
  


Reply



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
Shell scripting - Getting just directory string from file path string? arashi256 Programming 5 10-16-2009 08:21 AM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
creating array in c++ with string index mohtasham1983 Programming 3 03-11-2007 04:01 PM
How to initialize index with string in shell programming jayakumar Programming 3 12-08-2006 01:09 AM
C - Printing ELF String Table Index nixnoobie Programming 3 04-01-2005 06:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:03 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
Open Source Consulting | Domain Registration