[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Simple "print" in END condition prints last line
From: |
Dave B |
Subject: |
Re: Simple "print" in END condition prints last line |
Date: |
Fri, 29 May 2009 00:43:29 +0200 |
User-agent: |
Thunderbird 2.0.0.21 (X11/20090527) |
James Kanze wrote:
> 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.
I would have sworn that POSIX said that in the END block $0 (just like NF,
NR and others) retains the value it had for the last record read (unless
getline is used, but that's not relevant here). However, I'm not able to
find anything in POSIX that indicates that, nor anything that indicates the
contrary.
It seems that other awk implementations behave like gawk, and match what
seems to me historical practice:
$ echo 'foo' | true_awk 'END{print}'
foo
$ echo 'foo' | mawk 'END{print}'
foo
$ echo 'foo' | busybox awk 'END{print}'
foo
It would be nice if there was a clearer statement in POSIX. The GNU awk
documentation however does shed a bit of light:
"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.
The third point follows from the first two. The meaning of ‘print’ inside a
BEGIN or END rule is the same as always: ‘print $0’. If $0 is the null
string, then this prints an empty line. Many long time awk programmers use
an unadorned ‘print’ in BEGIN and END rules, to mean ‘print ""’, relying on
$0 being null. Although one might generally get away with this in BEGIN
rules, it is a very bad idea in END rules, at least in gawk. It is also poor
style, since if an empty line is needed in the output, the program should
print one explicitly."
Hope that helps.
--
D.