Re: OT: matched string of a regular expression
kamaraju kusumanchi wrote:
> Consider the following file.
>> cat junk.txt
> 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
> 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
> where the second ??? is the string matched by the first regular
> 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.
assuming foo.dat has:
> cat foo.dat
$> sed -e 's/\(a(.*)\)1b/\123b/' foo.dat > foo_new.dat
$> 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
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.