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.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
DimitriosP
  • 106
  • 1
  • 8

2 Answers2

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
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 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`? – Ed Morton Aug 01 '16 at 13:40
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 :)

Diego
  • 1,789
  • 10
  • 19
  • 1
    This sounds logical, but it is not the case. In my GNU awk 4.1, for example, `echo "a" | awk 'END {print}'` returns `a`. – fedorqui Apr 30 '15 at 14:27
  • @fedorqui 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. – Peter - Reinstate Monica Apr 30 '15 at 14:29
  • That works on my system. Thank you guys for your quick replies! – DimitriosP Apr 30 '15 at 14:32
  • @PeterSchneider I just found the reference in the GNU awk guide. Apparently this was the old behaviour of `awk`, now kind of deprecated. – fedorqui Apr 30 '15 at 14:34
  • @fedorqui He's working on HP-UX, it's probably different there. I can't find anything about it in the POSIX documentation right now :S – Diego Apr 30 '15 at 14:35
  • Yes im on HP-UX 2009 version!! – DimitriosP Apr 30 '15 at 14:36
  • @Diego see my answer, I found the reference of this behaviour. – fedorqui Apr 30 '15 at 14:36