Fields defined in the END block in AWK?

What happens when using $1 , $2 ... in an END block, for example:

 awk '{print $3}END{print $1 $2}' 

I found that $1 and $2 store the values ​​from the last record. Is this behavior a guaranteed standard or is it implementation specific?

+7
variables bash awk gawk
source share
1 answer

Checking the docs , we see that it is implementation specific :

Traditionally, mainly due to implementation problems, $ 0 and NF were undefined inside the END rule. The POSIX standard indicates that NF is available in the END rule. It contains the number of fields from the last entry entry. Most likely, due to oversight, the standard does not say that $ 0 is also preserved, although logically one should think what it should be. In fact, all BWK awk, mawk and gawk save the value $ 0 for use in END rules. Remember, however, that some other implementations and many older versions of Unix awk are not .

+8
source share

All Articles