Are fields defined in the END block in AWK?

人走茶凉 提交于 2019-12-08 17:28:15

问题


What would happen when using $1, $2 ... in the END block, like:

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

I found that $1 and $2 retain the values from the last record. Is this behaviour guaranteed by the standard or is it implementation-specific?


回答1:


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

Traditionally, due largely to implementation issues, $0 and NF were undefined inside an END rule. The POSIX standard specifies that NF is available in an END rule. It contains the number of fields from the last input record. Most probably due to an oversight, the standard does not say that $0 is also preserved, although logically one would think that it should be. In fact, all of BWK awk, mawk, and gawk preserve the value of $0 for use in END rules. Be aware, however, that some other implementations and many older versions of Unix awk do not.



来源:https://stackoverflow.com/questions/38698645/are-fields-defined-in-the-end-block-in-awk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!