473,385 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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

May 8 '06 #1
3 1898
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.

May 8 '06 #2
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)")""") print se (string)
192.168.1.17
192.168.1.93
----- Original Message -----
From: "Hari" <p.*********@gmail.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Monday, May 08, 2006 12:18 PM
Subject: How to get a part of string which follows a particular pattern
usingshell script

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


May 12 '06 #3
Anthra Norell wrote:
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.

--
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net
May 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kali K E | last post by:
Hi, I wanted to grep a file for a pattern. The pattern is a dynamic one stored in the string "rec". I tried to do it as follows: cmd = "grep -c rec filelist2 > grepdata" os.system(cmd) But...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
6
by: David M. Wilson | last post by:
Hello fellow users! I've been using Python in a couple of different environments for a few years now. I have quite often found that I have needed to deal with filenames which may contain...
42
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
10
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script. I want to extract information from a...
2
nithinpes
by: nithinpes | last post by:
I tried the following regular expression in script to extract lines not begining with capital letters and not ending with numbers. if ($str =~/^.+$/) ## similarly /^.+\D$/ { ######...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.