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

2006-05-08 Thread Hari
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.


So please help me.


A bunch of thanks in advance. 


Regards, 
P.R.Hariram

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2006-05-08 Thread nikie
Hari wrote:

 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.

Regular Expressions are probably the easiest way to do this. Example:

import re

str = 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)

m = re.search(addr=(\d+\.\d+\.\d+\.\d+), str)
print m.group(1)

- Prints: 192.168.1.17

Read the Python manual on Regular Expressions (module re) or google for
Python regular expression tutorials if your search is more complex than
this (or if you want to know what's going on). If you want a good
in-depth text on the subject, I'd recommend J. Friedls Mastering
Regular Expressions.

Have fun.

-- 
http://mail.python.org/mailman/listinfo/python-list