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

H

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
 
N

nikie

Hari said:
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.
 
A

Anthra Norell

This is the third problem today which I propose to solve with my stream
editor SE. If the group thinks it could be useful I would submit it the
moment I'm done with the doc, which is in the final stage.

Frederic
se = SE.SE ('<EAT> ~addr=[0-9.]+~==(10) | addr\== ')
string = """(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)")""")
192.168.1.17
192.168.1.93


----- Original Message -----
From: "Hari" <[email protected]>
Newsgroups: comp.lang.python
To: <[email protected]>
Sent: Monday, May 08, 2006 12:18 PM
Subject: How to get a part of string which follows a particular pattern
usingshell script
 
E

Edward Elliott

Anthra said:
This is the third problem today which I propose to solve with my stream
editor SE. If the group thinks it could be useful I would submit it the
moment I'm done with the doc, which is in the final stage.

You keep saying that and getting no response, so I'll take a stab at telling
you why. I take it that when you say 'propose it to the group' and 'submit
it for distribution', you mean include it in the standard library.

My impression (and fervent hope) is that nobody's gonna throw a new,
unfamiliar module straight into the standard library. Put it up somewhere
like the cheese shop and let people kick the tires for a few months/years.
Then if it solves a real need, works well, is sufficiently general, and
isn't handled better by another module, it might be eligible for
consideration for inclusion in the standard library. New modules aren't
just thrown in there willy-nilly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top