awk's END block behaviour on HP-UX
Asked Answered
A

2

1

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.

Achromat answered 30/4, 2015 at 14:19 Comment(5)
What version of awk are you using? Does printing the contents of the file with awk ('{print}') work correctly?Manns
My awk (reports "GNU Awk 4.1.1, API: 1.1 (GNU MPFR 3.1.2, GNU MP 6.0.0) Copyright (C) 1989, 1991-2014 Free Software Foundation") prints the last line.Divulgence
Try this: awk '{x=$0}END{print x}' FileGaiseric
So it means that $0 is the current line?Achromat
Nit: awk is not "returning" an empty string. It "outputs" a single newline and "returns" an integer value, probably zero.Evenfall
S
3

END means "execute the given block after the file has been processed", there is no data to print associated to it.

If you want to process the last line, save each line in a variable in a default block and then process the variable in the end block.

awk '{ last_line = $0; } END { /* do something with last_line */}' file

Or use tail before feeding data to awk :)

Sarto answered 30/4, 2015 at 14:26 Comment(7)
This sounds logical, but it is not the case. In my GNU awk 4.1, for example, echo "a" | awk 'END {print}' returns a.Rockaway
@Rockaway Same here, as noted above. Reading the man page it's kindof underspecified whether $0 is preserved after EOF has been encountered. I guess Rahul's and Diego's solution of assigning each line to a variable and printing that then is robust.Divulgence
That works on my system. Thank you guys for your quick replies!Achromat
@PeterSchneider I just found the reference in the GNU awk guide. Apparently this was the old behaviour of awk, now kind of deprecated.Rockaway
@Rockaway He's working on HP-UX, it's probably different there. I can't find anything about it in the POSIX documentation right now :SSarto
Yes im on HP-UX 2009 version!!Achromat
@Sarto see my answer, I found the reference of this behaviour.Rockaway
R
5

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
Rockaway answered 30/4, 2015 at 14:33 Comment(1)
Can someone with HP-UX test if echo 'a b c' | awk '{last=$0} END{$0=last; print NF, $0, $1}' file outputs 3 a b c a?Colton
S
3

END means "execute the given block after the file has been processed", there is no data to print associated to it.

If you want to process the last line, save each line in a variable in a default block and then process the variable in the end block.

awk '{ last_line = $0; } END { /* do something with last_line */}' file

Or use tail before feeding data to awk :)

Sarto answered 30/4, 2015 at 14:26 Comment(7)
This sounds logical, but it is not the case. In my GNU awk 4.1, for example, echo "a" | awk 'END {print}' returns a.Rockaway
@Rockaway Same here, as noted above. Reading the man page it's kindof underspecified whether $0 is preserved after EOF has been encountered. I guess Rahul's and Diego's solution of assigning each line to a variable and printing that then is robust.Divulgence
That works on my system. Thank you guys for your quick replies!Achromat
@PeterSchneider I just found the reference in the GNU awk guide. Apparently this was the old behaviour of awk, now kind of deprecated.Rockaway
@Rockaway He's working on HP-UX, it's probably different there. I can't find anything about it in the POSIX documentation right now :SSarto
Yes im on HP-UX 2009 version!!Achromat
@Sarto see my answer, I found the reference of this behaviour.Rockaway

© 2022 - 2024 — McMap. All rights reserved.