bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gawk version 3.1.0 will not print a ";"


From: Aharon Robbins
Subject: Re: gawk version 3.1.0 will not print a ";"
Date: Wed, 23 Jan 2002 14:07:44 +0200

Greetings.  Re this:

> From: "Mandeep Chadha" <address@hidden>
> To: <address@hidden>
> Subject: gawk version 3.1.0 will not print a ";"
> Date: Tue, 22 Jan 2002 17:23:57 -0600
>
> The file "tmp" contains the following lines:
>
> A
> B
> C
> D
>
> and when I run the command:
>
>       gawk '{print "Input = "$_" ; "}' tmp
>
> I get the following output:
>
> Input = A
> Input = B
> Input = C
> Input = D
>
> while I expect the following output:
>
> Input = A ;
> Input = B ;
> Input = C ;
> Input = D ;

There is an interesting interaction of features here.  If you rewrite
your program as so:

        gawk '{print "Input = " $_ " ; "}' tmp

it will work correctly.  The extra spaces force gawk to see the string
concatenation in the way you want it to happen.

What's happening is that gawk is parsing the original program as:

        print ( ("Input = ")  $(_" ; ") )

Gawk interprets   _" ; "   as a string with potential to be translated.
(This is a new feature in 3.1.0, see the documentation.) When used as a
number for use with the field operator `$', this string acts as numeric 0.
With the --lint option, gawk will warn you:

    gawk: cmd. line:1: (FILENAME=/tmp/x FNR=1) warning: attempt to field 
reference from non-numeric value

If you use `gawk --posix' on the original program, it will also be parsed
"correctly."

I suspect that the real root of the problem is your use of `$_'.  awk is
not perl, and the meaning of `$_' is "take the numeric value of the
variable named `_' and use that as the number of the field to retrieve."
The variable `_' is automatically initialized to zero, making `$_'
the same as `$0', but only by good luck.

For both clarity and correctness, you should probably use printf:

        gawk '{ printf "Input = %s ; \n", $0 }' tmp

Good luck,

Arnold Robbins



reply via email to

[Prev in Thread] Current Thread [Next in Thread]