bug-gawk
[Top][All Lists]
Advanced

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

Re: Use of '()' in a regexp


From: arnold
Subject: Re: Use of '()' in a regexp
Date: Wed, 06 Jan 2021 05:15:48 -0700
User-agent: Heirloom mailx 12.5 7/5/10

Yes. It's a bug. I have to figure out what the problem is but don't have
time at the moment.

Arnold

Manuel Collado <mcollado2011@gmail.com> wrote:

> Well, it seems that RS='()' reads the whole file as a single record, but 
> discards the last character, whatever is it.
>
> $ printf 'foo\nbar' | awk -v RS='()' '{print "<"RT"><"$0">"}'
> <><foo
> ba>
>
> Forcing to match at the end of the file doesn't consumes the last character.
>
> $ printf 'foo\nbar' | awk -v RS='()$' '{print "<"RT"><"$0">"}'
> <><foo
> bar>
>
> It looks like a bug.
>
> Regards.
>
>
> El 05/01/2021 a las 21:02, Ed Morton escribió:
> > Hmm, it's not quite behaving the same as `RS='\n$'` as `RS='()'` doesn't
> > populate `RT` with the terminating `\n` though it does consume it:
> >
> > $ printf 'foo\n' | awk -v RS='()' -v ORS='x\n' '{print "<"RT">"}1'
> > <>x
> > foox
> >
> > $ printf 'foo\n' | awk -v RS='\n$' -v ORS='x\n' '{print "<"RT">"}1'
> > <
> >  >x
> > foox
> >
> >
> > On 1/5/2021 12:38 PM, Ed Morton wrote:
> >> Someone just pointed this out to me (gawk 5.1.0):
> >>
> >> $ printf 'foo\n' | awk '{gsub(/()/,"x")} 1'
> >> xfxoxox
> >>
> >> $ printf 'foo\n' | awk -v RS='()' -v ORS='x\n' '1'
> >> foox
> >>
> >> Obviously that's a pretty ridiculous regexp but it still has me
> >> wondering - why does `gsub()` treat the regexp `()` as matching a null
> >> string around every character while `RS` treats it as if I'd asked it
> >> to match the `\n` at the end of the input:
> >>
> >> $ printf 'foo\n' | awk -v RS='\n$' -v ORS='x\n' '1'
> >> foox
> >>
> >> I could just file this under "don't write stupid regexps" but I was
> >> wondering if there's a more concrete, satisfying explanation of the
> >> behavior.
> >>
> >>     Ed.
> >
>
>
> -- 
> Manuel Collado - http://mcollado.z15.es



reply via email to

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