bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] Fwd: why does referencing NF or different fields change $


From: arnold
Subject: Re: [bug-gawk] Fwd: why does referencing NF or different fields change $0 after recompilation?
Date: Thu, 17 Sep 2015 07:50:13 -0600
User-agent: Heirloom mailx 12.4 7/29/08

Interesting bug. Thanks for the report. I'll look into it.

Arnold

Ed Morton <address@hidden> wrote:

> I got a couple of responses to my question below on comp.lang.awk and I'm now 
> pretty confident it's a bug and not my misunderstanding of something.
>
> Josef Frank pointed out that:
>
> > Reminds me of a bug (in gawk 4.0.0) mentioned at the top of:
> > http://git.savannah.gnu.org/cgit/gawk.git/tree/test/pty1.awk
>
> Can you take a look?
>
>       Ed.
>
> -------- Forwarded Message --------
> Subject: why does referencing NF or different fields change $0 after 
> recompilation?
> Date: Wed, 16 Sep 2015 22:15:05 -0500
> From: Ed Morton <address@hidden>
> Organization: A noiseless patient Spider
> Newsgroups: comp.lang.awk
>
> This GNU awk script is intended to replace commas with semi-colons in a CSV 
> file
> that could contain commas in the quoted fields and could have blank fields:
>
> $ awk -v FPAT='([^,]*)|("[^"]+")' -v OFS=';' '{$1=$1}1' file
>
> Can anyone explain why the first call to awk below replaces the first comma 
> with
> two semi-colons while the second and third (which are only different from the
> first in that they mentions NF somewhere in the action block) replace it with
> one, which is the desired result?
>
> $ cat file
> "A","B","C"
>
> $ awk -v FPAT='([^,]*)|("[^"]+")' -v OFS=';' '{$1=$1}1' file
> "A";;"B";"C"
>
> $ awk -v FPAT='([^,]*)|("[^"]+")' -v OFS=';' '{NF;$1=$1}1' file
> "A";"B";"C"
>
> $ awk -v FPAT='([^,]*)|("[^"]+")' -v OFS=';' '{$1=$1;NF}1' file
> "A";"B";"C"
>
> Mentioning some other variables has no effect:
>
> $ awk -v FPAT='([^,]*)|("[^"]+")' -v OFS=';' '{OFS;$1=$1}1' file
> "A";;"B";"C"
>
> $ awk -v FPAT='([^,]*)|("[^"]+")' -v OFS=';' '{$0;$1=$1}1' file
> "A";;"B";"C"
>
> while just mentioning $1/2/3 can change where the double-semi-colon appears:
>
> $ awk -v FPAT='([^,]*)|("[^"]+")' -v OFS=';' '{$1;$1=$1}1' file
> "A";;"B";"C"
> $ awk -v FPAT='([^,]*)|("[^"]+")' -v OFS=';' '{$2;$1=$1}1' file
> "A";"B";;"C"
> $ awk -v FPAT='([^,]*)|("[^"]+")' -v OFS=';' '{$3;$1=$1}1' file
> "A";"B";"C"
>
> and (presumably related) we get a different $0 depending on which field we
> assign to itself:
>
> $ awk -v FPAT='([^,]*)|("[^"]+")' -v OFS=';' '{$1=$1}1' file
> "A";;"B";"C"
> $ awk -v FPAT='([^,]*)|("[^"]+")' -v OFS=';' '{$2=$2}1' file
> "A";"B";;"C"
> $ awk -v FPAT='([^,]*)|("[^"]+")' -v OFS=';' '{$3=$3}1' file
> "A";"B";"C"
>
> I'm using
>
> $ awk --version
> GNU Awk 4.1.3, API: 1.1 (GNU MPFR 3.1.3, GNU MP 6.0.0)
>
> in bash on cygwin.
>
> Regards,
>
>       Ed.
>
>



reply via email to

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