Re: How to get a part of string which follows a particular pattern using shell script



"Hari" <p.r.hariram@xxxxxxxxx> writes:

Hi all,

I need to get a part of string which follows a pattern 'addr='


For example:


a)test="192.168.1.17:/home/ankur/nios_fson/mnt/tmptype
nfs(rw,addr=192.168.1.17)"
b)test="/dev/root on / typr nfs
(rw,v2,rsize=1024,wsize=1024,hard,udp,nolock,addr=192.168.1.93)"


I need to get the ipaddress from the above two strings a and b which
follows 'addr='. I tried to use cut, but it accepts only single charter

as delimiter. If I give delimiter as 'addr=' for cut command it gives
me a error.

The shell should be able to do it itself.
man bash

for test in \
"192.168.1.17:/home/ankur/nios_fson/mnt/tmptype nfs(rw,addr=192.168.1.17)" \
"/dev/root on / typr nfs (rw,v2,...,hard,udp,nolock,addr=192.168.1.93)" \
; do
tost="${test/*addr=}"
ip="${tost/)*}"
echo "$ip"
done


--
__Pascal Bourguignon__ http://www.informatimago.com/
Cats meow out of angst
"Thumbs! If only we had thumbs!
We could break so much!"
.



Relevant Pages