Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

why don't sequences of chars match in gsub()?
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-01-05 08:56 AM


Re: why don't sequences of chars match in gsub()?
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Anton Treuenfels
04-01-05 08:56 AM


Re: why don't sequences of chars match in gsub()?
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


Report this thread to moderator Post Follow-up to this message
Old Post
Manuel Collado
04-01-05 01:55 PM


Re: why don't sequences of chars match in gsub()?

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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-06-05 05:05 PM


Re: why don't sequences of chars match in gsub()?

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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-07-05 01:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:00 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.