Substring extraction using bash shell scripting and awk
Posted
by rohanbk
on Stack Overflow
See other posts from Stack Overflow
or by rohanbk
Published on 2010-04-03T23:17:42Z
Indexed on
2010/04/03
23:23 UTC
Read the original article
Hit count: 266
So, I have a file called 'dummy' which contains the string:
"There is 100% packet loss at node 1".
I also have a small script that I want to use to grab the percentage from this file. The script is below.
result=`grep 'packet loss' dummy` |
awk '{ first=match($0,"[0-9]+%")
last=match($0," packet loss")
s=substr($0,first,last-first)
print s}'
echo $result
I want the value of $result to basically be 100% in this case. But for some reason, it just prints out a blank string. Can anyone help me?
© Stack Overflow or respective owner