FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 03-04-2009, 08:37 PM
Florian Kulzer
 
Default OT: matched string of a regular expression

On Wed, Mar 04, 2009 at 16:24:27 -0500, kamaraju kusumanchi wrote:

[...]

> Now,
> I would like to replace all the occurrences of
>
> a(ijk)1b with a(ijk)23b
> a(jik)1b with a(jik)23b
> a(ikj)1b with a(ikj)23b
> and so on for all the strings such as a(???)
>
> Because of the size of the files involved, the number of files on
> which I have to perform this operation I decided to use sed (instead
> of doing it manually in vim)

[...]

> Is there any way
> to write something like
>
> s/a(???)1b/a(???)23b/g
>
> where the second ??? is the string matched by the first regular
> expression.

sed -e 's/a((...))1b/a(1)23b/g'

--
Regards, | http://users.icfo.es/Florian.Kulzer
Florian |


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 03-04-2009, 08:45 PM
David Jardine
 
Default OT: matched string of a regular expression

On Wed, Mar 04, 2009 at 04:24:27PM -0500, kamaraju kusumanchi wrote:
> Consider the following file.
>
> > cat junk.txt
> a(i)1b
> a(j)1b
> a(ij)1b
> a(ji)1b
> a(ijk)1b
> a(jik)1b
> a(ikj)1b
> a(jki)1b
> a(kij)1b
> a(kji)1b
>
> In general the file is very long, might contain some other text. Now,
> I would like to replace all the occurrences of
>
> a(ijk)1b with a(ijk)23b
> a(jik)1b with a(jik)23b
> a(ikj)1b with a(ikj)23b
> and so on for all the strings such as a(???)
>
> Because of the size of the files involved, the number of files on
> which I have to perform this operation I decided to use sed (instead
> of doing it manually in vim)
>
> However my sed script currently involves 6 lines (for all the
> combinations of ijk) such as
>
> s/a(ijk)1b/a(ijk)23b/g
> s/a(jik)1b/a(jik)23b/g
> s/a(ikj)1b/a(ikj)23b/g
> and so on
>
> This method is very cumbersome, not scalable. If I have to do similar
> operations on a(ijklm) the script would be 120 lines! Is there any way
> to write something like
>
> s/a(???)1b/a(???)23b/g
>
> where the second ??? is the string matched by the first regular
> expression.
>
> In general, how can I obtain the string that is matched by a regular
> expression (in a shell script)?
>
> BTW, Is sed the right tool for this kind of job? If not, can you
> suggest any other tool that will get the job done in less time?

Perhaps you could write a script/program to permutate the letters of
the command line argument and then do:

for jim in `permutate`;do sed -i "s/a$jim1b/a$jim23b/";done

I've no idea if this would work. I'd certainly skip the -i option to
start with

>
> PS: Please include my email in the CC.
>
> thanks
> raju
>
>
> --
> To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
>

--
"Running Debian/GNU Linux and
loving every minute of it." -L. von Sacher-M. (1835-1895)


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 03-04-2009, 08:52 PM
"H.S."
 
Default OT: matched string of a regular expression

kamaraju kusumanchi wrote:
> Consider the following file.
>
>> cat junk.txt
> a(i)1b
> a(j)1b
> a(ij)1b
> a(ji)1b
> a(ijk)1b
> a(jik)1b
> a(ikj)1b
> a(jki)1b
> a(kij)1b
> a(kji)1b
>
> In general the file is very long, might contain some other text. Now,
> I would like to replace all the occurrences of
>
> a(ijk)1b with a(ijk)23b
> a(jik)1b with a(jik)23b
> a(ikj)1b with a(ikj)23b
> and so on for all the strings such as a(???)
>
> Because of the size of the files involved, the number of files on
> which I have to perform this operation I decided to use sed (instead
> of doing it manually in vim)
>
> However my sed script currently involves 6 lines (for all the
> combinations of ijk) such as
>
> s/a(ijk)1b/a(ijk)23b/g
> s/a(jik)1b/a(jik)23b/g
> s/a(ikj)1b/a(ikj)23b/g
> and so on
>
> This method is very cumbersome, not scalable. If I have to do similar
> operations on a(ijklm) the script would be 120 lines! Is there any way
> to write something like
>
> s/a(???)1b/a(???)23b/g
>
> where the second ??? is the string matched by the first regular
> expression.
>
> In general, how can I obtain the string that is matched by a regular
> expression (in a shell script)?
>
> BTW, Is sed the right tool for this kind of job? If not, can you
> suggest any other tool that will get the job done in less time?
>
> PS: Please include my email in the CC.
>
> thanks
> raju
>
>

assuming foo.dat has:
> cat foo.dat
a(i)1b
a(j)1b
a(ij)1b
a(ji)1b
a(ijk)1b
a(jik)1b
a(ikj)1b
a(jki)1b
a(kij)1b
a(kji)1b

Then
$> sed -e 's/(a(.*))1b/123b/' foo.dat > foo_new.dat

Or just:
$> sed -e 's/1b/23b/' foo.dat > foo_new.dat

if only 1b is to be replaced. Same thing using perl (will replace
contents of foo.dat inplace):
$> perl -p -i -e "s/1b/23b/g;" foo.dat


Regards
--

Please reply to this list only. I read this list on its corresponding
newsgroup on gmane.org. Replies sent to my email address are just
filtered to a folder in my mailbox and get periodically deleted without
ever having been read.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 

Thread Tools




All times are GMT. The time now is 10:52 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org