bug-gawk
[Top][All Lists]
Advanced

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

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


From: Ed Morton
Subject: [bug-gawk] Fwd: why does referencing NF or different fields change $0 after recompilation?
Date: Thu, 17 Sep 2015 08:04:10 -0500
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0

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]