是在AWK END块定义的字段? [英] Are fields defined in the END block in AWK?

查看:199
本文介绍了是在AWK END块定义的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 $ 1 $ 2 ...在结束的时候<会发生什么/ code>块,如:

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

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

我发现 $ 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?

推荐答案

检查<一个href=\"https://www.gnu.org/software/gawk/manual/html_node/I_002fO-And-BEGIN_002fEND.html#I_002fO-And-BEGIN_002fEND\"相对=nofollow>文档我们看到它为实现特定

传统上,主要是因为执行问题,$ 0和NF是
  未定义一个END规则中。 POSIX标准的规定,NF是
  在最终规则可用。它包含从字段数
  最后输入记录。最可能是由于疏忽,标准
  不说,$ 0也preserved,虽然逻辑上是一个会
  认为是应该的。事实上,所有BWK awk中,mawk,并呆子
  preserve $ 0在END规则使用的值。 请注意,但是,
  一些其他的实现和许多老版本的Unix的awk不

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.

这篇关于是在AWK END块定义的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆