For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2005 > why don't sequences of chars match in gsub()?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author why don't sequences of chars match in gsub()?
Ed Morton

2005-04-01, 3:56 am

Can anyone explain why the output of this command:

echo "a:::b" | awk 'gsub("::",":x:")'

is this:

a:x::b

instead of this:

a:x:x:b

The above happens with all awks I've tested (and sed) so I know it's not
just a tool issue and I could just accept it and move on, but I'd really
like to know the gory details.

I know I can produce the second output by doing:

echo "a:::b" | awk 'gsub("::",":x:")gsub("::",":x:")'

but is there any way to get that second output with a single pass of
gsub() or gensub()?

Ed.
Anton Treuenfels

2005-04-01, 3:56 am

"Ed Morton" <morton@lsupcaemnt.com> wrote in message
news:EeWdna2wkrwyKdHfRVn-vA@comcast.com...
> Can anyone explain why the output of this command:
>
> echo "a:::b" | awk 'gsub("::",":x:")'
>
> is this:
>
> a:x::b
>
> instead of this:
>
> a:x:x:b


gsub() looks for "non-overlapping" matches only (you probably know that
already).

There are only three colon characters in "a:::b". The first match to "::"
eats two of them, and then there is only one left (so no further match).

- Anton Treuenfels


Manuel Collado

2005-04-01, 8:55 am

Anton Treuenfels wrote:

> "Ed Morton" <morton@lsupcaemnt.com> wrote in message
> news:EeWdna2wkrwyKdHfRVn-vA@comcast.com...
>
>
>
> gsub() looks for "non-overlapping" matches only (you probably know that
> already).
>
> There are only three colon characters in "a:::b". The first match to "::"
> eats two of them, and then there is only one left (so no further match).


Yes. gsub doesn't rescan replaced text. You may want to use:

while( gsub("::",":x:") );

--
To reply by e-mail, please remove the extra dot
in the given address: m.collado -> mcollado

Ed Morton

2005-04-06, 12:05 pm



Manuel Collado wrote:
> Anton Treuenfels wrote:
>
>
>
> Yes. gsub doesn't rescan replaced text. You may want to use:
>
> while( gsub("::",":x:") );
>


Nice. You confirmed what I saw, and I suppose the rationale behind it is
that you're guaranteed to avoid recursion problems as you'd get with a
function that tries to do the equivalent of:

echo "a:::b" | awk '{while (gsub("::","::"));}1'

Thanks to both of you for your responses.

Ed.
Ed Morton

2005-04-07, 8:55 am



Manuel Collado wrote:
> Anton Treuenfels wrote:
>
>
>
> Yes. gsub doesn't rescan replaced text. You may want to use:
>
> while( gsub("::",":x:") );
>


Nice. You confirmed what I saw, and I suppose the rationale behind it is
that you're guaranteed to avoid recursion problems as you'd get with a
function that tries to do the equivalent of:

echo "a:::b" | awk '{while (gsub("::","::"));}1'

Thanks to both of you for your responses.

Ed.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com