bug-gawk
[Top][All Lists]
Advanced

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

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


From: Sundeep Agarwal
Subject: Replacing Nth match with gensub when the regexp can match empty text
Date: Tue, 3 Aug 2021 10:56:24 +0530

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]