LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   hi .. i need a shell function to index a string .... (https://www.linuxquestions.org/questions/programming-9/hi-i-need-a-shell-function-to-index-a-string-897618/)

xunling 08-16-2011 10:14 AM

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?

TB0ne 08-16-2011 10:27 AM

Quote:

Originally Posted by xunling (Post 4444636)
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.

catkin 08-16-2011 10:52 AM

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 }

onebuck 08-16-2011 01:07 PM

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.

grail 08-17-2011 01:21 AM

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


All times are GMT -5. The time now is 05:49 PM.