Awk: use variable defined in begin

Hi there,

I'm working with file more than 400K lines, 60 columns. Column count is going to be multiple of 12: 60, 12, 72 or so.
NF/12 gives me on how many iterations I've to do to check certain value.

For example: 7, 14th if only 24 columns in file.
7th, 14th and 21st if 36 columns in file.

I'd like to store and use NF/12 after calculating in BEGIN. But I can't use it.


head CHR_10_paste.info | awk ' BEGIN{ incr=NF/12 } NR>1 { print $incr }'

incr is 0 so this one-liner prints all the columns.
How do I use it later on once calculated in BEGIN?

Try:

print incr

awk does not use a $ sign to get the value stored in the variable..

It prints 0

Oww that is right,
Try:

FNR==1{ incr=NF/12 }

If NF is the same for every line in the file

In the BEGIN section NF is not yet initialized..

And remove the NR>1 condition

So:

awk ' FNR==1{ incr=NF/12 } { print incr }'

In the BEGIN section, no file has been opened and read yet, so NF can't be set. And, if I understand correctly, NF changes with every line read, doesn't it? So incr should not be calculated in the BEGIN section, but for every single line.

NF is going to be constant in a file for all the rows.
A file may have a different number of columns. But they are going to be consistent across all the lines.
I hope I'm making sense.

That's why Scrutinizer suggests to set the incr in line 1.

NR==1{ incr=NF/12 } { print incr, "line", NR }

A next skips the further processing of the current line.

NR==1{ incr=NF/12; next } { print incr, "line", NR }