[Top][All Lists]

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

What is done when FS is set in an action?

From: Peng Yu
Subject: What is done when FS is set in an action?
Date: Mon, 21 Jun 2021 09:34:01 -0500


It seems that FS has to be set outside an action. Otherwise, the
result is not very meaningful.

$ gawk -v FS="x[ ]y[ ]=" -v OFS=, -e '{ NF=NF; print $1,$2 }' <<< 'ax y =b'
$ gawk -v OFS=, -e '{ FS="x[ ]y[ ]="; NF=NF; print $1,$2 }'  <<< 'ax y =b'

I don't think the man page covers this behavior. It sounds like NF=NF
uses the old FS instead of the new FS set in the action. This is quite
counterintuitive. Should this behavior be changed so that only the
latest set FS is used?

       As each input record is read, gawk splits the record into
fields, using the value of the FS  variable
       as  the field separator.  If FS is a single character, fields
are separated by that character.  If FS
       is the null string, then each individual character  becomes  a
separate  field.   Otherwise,  FS  is
       expected  to be a full regular expression.  In the special case
that FS is a single space, fields are
       separated by runs of spaces and/or tabs and/or newlines.  NOTE:
The value of IGNORECASE  (see  below)
       also affects how fields are split when FS is a regular
expression, and how records are separated when
       RS is a regular expression.


reply via email to

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