473,569 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=10 24,wsize=1024,h ard,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 1905
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=10 24,wsize=1024,h ard,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=10 24,wsize=1024,h ard,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=10 24,wsize=1024,h ard,udp,nolock, addr=192.168.1. 93)")""") print se (string)
192.168.1.17
192.168.1.93
----- Original Message -----
From: "Hari" <p.*********@gm ail.com>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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=10 24,wsize=1024,h ard,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
2006
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 I find that the system is not referencing the contents of rec data. There is no syntax error and also the grep itself does not crib.
9
7991
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., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
6
1539
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 characters that confuse the UNIX shell (space and double quote in particular). Quite a while ago I came up with two simple functions which allowed me to...
42
4045
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. I'm not interested on which language is better in *general*, just for my purpose. My area of research is in CAD algorithms, and I'm sensing the...
29
4293
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 substring before a pattern), AFTER (return substring after a pattern), and BETWEEN (return substring between 2 patterns). My questions are: 1. Can...
18
2246
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 then want to use the string on the same page. My script is: function checkRadio(field) { for(var i=0; i < field.length; i++) {
10
11822
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 string, separated by newline. For this i am using regex. The String is given: P48534 EXP value is: e-10 Q0543 EXP value is: 4e-07
2
1773
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$/ { ###### } This didn't work. The pattern condition was applied only to begining of string. The lines returned contained both digits & non-digits...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.