|
From: | Manuel Collado |
Subject: | Re: Simple "print" in END condition prints last line |
Date: | Thu, 28 May 2009 23:29:34 +0200 |
User-agent: | Thunderbird 2.0.0.19 (Windows/20081209) |
James Kanze escribió:
Not a serious problem, but a simple "print" in the END condition prints the last line, e.g.: awk 'BEGIN { printf( " " ) } ; /^#/ { next } ; { printf( " %s", $2 ) } ; END { print }' rebuild.cmds Where rebuild.cmds is: pagnu002 linux comp=gcc arch=i80x86 syst=linux padev001 suncc comp=suncc arch=sparc syst=solaris padev001 sunccstl comp=suncc-stlport arch=sparc syst=solaris padev001 gcc comp=gcc arch=sparc syst=solaris padev001 v9 comp=suncc arch=sparcv9 syst=solaris padev001 v9stl comp=suncc-stlport arch=sparcv9 syst=solaris # pahmg001 linux64 comp=gcc arch=x86_64 syst=linux (no leading spaces) outputs: linux suncc sunccstl gcc v9 v9stl# pahmg001 linux64 comp=gcc arch=x86_64 syst=linux instead of the desired: linux suncc sunccstl gcc v9 v9stl The obvious work-around: replace the ``print'' with ``print ""''. But print without an argument should output $0, and the END condition doesn't have a $0.
This is expected behaviour. Quoting the Gawk manual: ------------------- 6.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, gawk does preserve the value of $0 for use in END rules. Be aware, however, that Unix awk, and possibly other implementations, do not.
-------------------- Regards. -- Manuel Collado - http://lml.ls.fi.upm.es/~mcollado
[Prev in Thread] | Current Thread | [Next in Thread] |