Use of `NF` in awk command

https://www.gnu.org/software/gawk/manual/gawk.html#Options:

-v var=val
–assign var=val

Set the variable var to the value val before execution of the program begins.

https://www.gnu.org/software/gawk/manual/gawk.html#Fields:

NF is a predefined variable whose value is the number of fields in the current record. awk automatically updates the value of NF each time it reads a record.

In your first program, you execute {NF=3} after each line is read, overwriting NF.

In your second program, you initially set NF=3 via -v, but that value is overwritten by awk when the first line of input is read.

FS is different because awk never sets this variable. It will keep whatever value you give it.

Leave a Comment

tech