Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of `NF` in awk command

Tags:

awk

I am trying to understand what is the difference between two command (I was expecting same result from the two):

Case-I

echo 'one,two,three,four,five' |awk -v FS=,  '{NF=3}1'
one two three

Case-II

echo 'one,two,three,four,five' |awk -v FS=,  -v NF=3 '{$1=$1}1'
one two three four five

Here is my current understanding: $1=$1 is used to force awk to reconstruct and use the variables defined. I am assigning FS like -v FS="," which is in effect unlike -v NF=3 .

Question: Why NF=3 is not taking effect where as FS=, does.

like image 865
monk Avatar asked Nov 10 '17 06:11

monk


People also ask

What is NF and NR in awk?

NR: NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file. NF: NF command keeps a count of the number of fields within the current input record.

What does FS do in awk?

awk Built-in Variables FS - Field Separator The variable FS is used to set the input field separator. In awk , space and tab act as default field separators. The corresponding field value can be accessed through $1 , $2 , $3 ... and so on.

What is awk '{ print $2 }'?

awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output. xargs kill -${2:-'TERM'} takes the process IDs from the selected sidekiq processes and feeds them as arguments to a kill command.

What is NR and FNR in awk?

NR and FNR are two built-in awk variables. NR tells us the total number of records that we've read so far, while FNR gives us the number of records we've read in the current input file.


2 Answers

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.

like image 90
melpomene Avatar answered Oct 03 '22 10:10

melpomene


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.

Remember : whenever awk reads record/line/row, awk will parse fields by field separator FS (default single space), and will recalculate fields and update the same in variable NF.

Therefore, below one does not work.

Why this doesn't work ?

  1. You defined NF, which is before the execution of the program
  2. awk read record/line/row, parsed fields, recalculated fields, so variable NF overwritten.

case - 1 :

echo 'one,two,three,four,five' |awk -v FS=,  -v NF=3 '{$1=$1}1'
one two three four five

Why this works ?

  1. awk read record/line/row, parsed fields, calculated fields, NF will be 5
  2. you have overwritten variable NF

case -2 :

echo 'one,two,three,four,five' |awk -v FS=,  '{   NF=3  }1'
one two three
                                                  ^
                                        Because you have overwritten variable



$ echo 'one,two,three,four,five' |awk -v FS=,  '{print "Before:"NF;  NF=3; print "After:"NF}1'
Before:5
After:3
one two three
like image 24
Akshay Hegde Avatar answered Oct 03 '22 09:10

Akshay Hegde