awk 在 HP-UX 上的 END 块行为
awk's END block behaviour on HP-UX
为什么 awk 'END{print}' file
return 是一个空字符串?
我检查了文件,它没有以空行结尾。
我在使用 HP-UX。
END
表示"execute the given block after the file has been processed",没有与之关联的打印数据。
如果要处理最后一行,将每一行保存在默认块中的变量中,然后在结束块中处理变量。
awk '{ last_line = [=10=]; } END { /* do something with last_line */}' file
或者在向 awk
提供数据之前使用 tail
:)
来自The GNU Awk guide - 7.1.4.2 Input/Output from BEGIN and END Rules
Traditionally, due largely to implementation issues, [=13=] 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 [=13=] 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 [=13=] for use in END rules. Be aware, however, that
some other implementations and many older versions of Unix awk do not.
因此,一般来说,END
现在包含最后一个 [=12=]
,而在您的 [旧] awk 版本中它不包含。
例如,我的 GNU Awk 确实以 "new" 方式工作:
$ awk --version
GNU Awk 4.1.0, API: 1.0
$ seq 10 | awk 'END {print}'
10
为什么 awk 'END{print}' file
return 是一个空字符串?
我检查了文件,它没有以空行结尾。
我在使用 HP-UX。
END
表示"execute the given block after the file has been processed",没有与之关联的打印数据。
如果要处理最后一行,将每一行保存在默认块中的变量中,然后在结束块中处理变量。
awk '{ last_line = [=10=]; } END { /* do something with last_line */}' file
或者在向 awk
提供数据之前使用 tail
:)
来自The GNU Awk guide - 7.1.4.2 Input/Output from BEGIN and END Rules
Traditionally, due largely to implementation issues, [=13=] 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 [=13=] 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 [=13=] for use in END rules. Be aware, however, that some other implementations and many older versions of Unix awk do not.
因此,一般来说,END
现在包含最后一个 [=12=]
,而在您的 [旧] awk 版本中它不包含。
例如,我的 GNU Awk 确实以 "new" 方式工作:
$ awk --version
GNU Awk 4.1.0, API: 1.0
$ seq 10 | awk 'END {print}'
10