bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] FPAT $field assignment garbages $0


From: Neil R. Ormos
Subject: Re: [bug-gawk] FPAT $field assignment garbages $0
Date: Mon, 13 Aug 2018 12:43:16 -0500 (CDT)

address@hidden wrote:
> Alex fxmbsw7 Ratchev <address@hidden> wrote:

>> echo foo bar | awk -v FPAT=foo '{ $1 = "za"; print }'
>> za

>> GNU Awk 4.2.1, API: 2.0

>> 'za bar' was expected

> You are misunderstanding what FPAT does. It describes the fields, not
> what is between them:

>       $ gawk --version
>       GNU Awk 4.2.1, API: 2.0 (GNU MPFR 3.1.4, GNU MP 6.1.0)

>       $ echo foo bar foo | gawk -v FPAT=foo '{ print NF }'
>       2

>       $ echo foo bar foo | gawk -v FPAT=foo '{ print $1, $2 }'
>       foo foo

> In the example you give, there is only one field, and your assignment
> replaces it.

> In short, gawk is working as expected.

The OP's report is also be consistent with
misunderstanding how $0 is rebuilt when any of the
fields is changed.

Someone who is not aware that $0 is rebuilt in its
entirety, using the current values of all of the
fields and OFS, whenever any field is changed,
might instead expect that $0 remain unchanged
except for the new value which has been assigned
to $1, resulting in the "za bar" output in
the OP's example.

Perhaps a slight change in the manual could make
the entire-$0-is-recalculated behavior more clear
by explicitly stating it earlier, apart from the
"there are times when it is convenient" language,
e.g., by adding the sentence,

  "In fact, awk rebuilds the entire $0 record, using
   the current values of the fields and OFS, each
   time any field is changed."

immediately after the sentence ending in "the
altered field."



reply via email to

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