Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

shell - awk's END block behaviour on HP-UX

Why does awk 'END{print}' file return an empty string?

I've checked the file and it does not end with empty line.

I'm on HP-UX.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From The GNU Awk guide - 7.1.4.2 Input/Output from BEGIN and END Rules

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.

So, in general, END now contains the last $0, whereas in your [old] awk version it does not.

For example, my GNU Awk does work in the "new" way:

$ awk --version
GNU Awk 4.1.0, API: 1.0
$ seq 10 | awk 'END {print}'
10

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...