bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Replacing Nth match with gensub when the regexp can match empty text


From: arnold
Subject: Re: Replacing Nth match with gensub when the regexp can match empty text
Date: Wed, 04 Aug 2021 04:18:41 -0600
User-agent: Heirloom mailx 12.5 7/5/10

Hi.

Thanks for the bug report.  This is an interesting difference.
I think it's a sed bug; the null string can match before the
first character, after the last one, and between adjacent
characters.

I will look up the manual and see if there's more that I can add
there.

Thanks,

Arnold

Sundeep Agarwal <learnbyexample.net@gmail.com> wrote:

> Hello.
>
> Replacing Nth match with gensub seems to differ compared to sed if the
> regexp can match empty text. I'm using GNU Awk 5.1.0 and GNU sed 4.8
>
> $ echo 'a,,c,d,,f' | sed 's/[^,]*/b/2'
> a,b,c,d,,f
> $ echo 'a,,c,d,,f' | sed 's/[^,]*/e/5'
> a,,c,d,e,f
>
> $ echo 'a,,c,d,,f' | awk '{print gensub(/[^,]*/, "b", 2)}'
> ab,,c,d,,f
> $ echo 'a,,c,d,,f' | awk '{print gensub(/[^,]*/, "e", 5)}'
> a,,ce,d,,f
>
> Replacing all the matches using "g" option works as expected:
>
> $ echo 'a,,c,d,,f' | sed 's/[^,]*/X/g'
> X,X,X,X,X,X
> $ echo 'a,,c,d,,f' | awk '{print gensub(/[^,]*/, "X", "g")}'
> X,X,X,X,X,X
>
> I'm not sure if this is related to the "Matching the Null String" example
> given in the manual:
>
> $ echo 'abc' | sed 's/m*/X/g'
> XaXbXcX
> $ echo 'abc' | awk '{gsub(/m*/, "X")} 1'
> XaXbXcX
>
> If the above gensub behavior for Nth match is working as expected, I'd
> suggest to add a note in the manual (unless I missed it).
>
> Regards,
> Sundeep



reply via email to

[Prev in Thread] Current Thread [Next in Thread]