bin/180328: awk(1) fails to treat var as integer
Steffen Daode Nurpmeso
sdaoden at gmail.com
Wed Jul 10 09:10:01 UTC 2013
The following reply was made to PR bin/180328; it has been noted by GNATS.
From: Steffen "Daode" Nurpmeso <sdaoden at gmail.com>
To: bug-followup at FreeBSD.org
Cc:
Subject: Re: bin/180328: awk(1) fails to treat var as integer
Date: Wed, 10 Jul 2013 11:01:34 +0200
Hello, i'm forwarding one more. (This time to bug-followup@ --
hello, Mark Linimon!)
-------- Original Message --------
Date: Wed, 10 Jul 2013 10:53:13 +0200
From: Steffen "Daode" Nurpmeso <sdaoden at gmail.com>
To: gnats-bugs at NetBSD.org
Subject: Re: bin/48017: awk(1) fails to treat var as integer (may be related
to #47840)
David Holland <dholland-bugs at netbsd.org> wrote:
| sprintf witih %d doesn't produce an number value; it produces a
| string value, which you have to coerce to a number by adding zero to
| it to get it to behave like a number.
(Adding +0 was my final solution too, because GNU awk(1) didn't
make it by the (presumably more expensive, too) sprintf("%X")
call just as all other tested awk(1)s did.)
So there is a problem with the implicit type conversion, since
echo f001 f00d |\
awk '{ a=sprintf("%d", "0x" $1); b=sprintf("%d", "0x" $2); while (a < b) { print a; a++; }}'
works just fine?!? I think the relevant parts from POSIX are
the value of an expression shall be implicitly converted to the
type needed for the context in which it is used.
[.]
A numeric value that is exactly equal to the value of an integer
(see Concepts Derived from the ISO C Standard) shall be converted
to a string by the equivalent of a call to the sprintf function
(see String Functions) with the string "%d" as the fmt argument
and the numeric value being converted as the first and only expr
argument.
[.]
This volume of POSIX.1-2008 specifies no explicit conversions
between numbers and strings. An application can force an
expression to be treated as a number by adding zero to it, or can
force it to be treated as a string by concatenating the null
string ( "" ) to it.
[.]
A string value shall be considered a numeric string if it comes
from one of the following:
[.]
1. Field variables
[.]
8. Variable assignment from another numeric string variable
[...]
and an implementation-dependent condition corresponding to either
case (a) or (b) below is met.
[.]
b. After all the following conversions have been applied, the
resulting string would lexically be recognized as a NUMBER
token as described by the lexical conventions in Grammar :
[.]
Whether or not a string is a numeric string shall be relevant only
in contexts where that term is used in this section.
And because the `Table: Expressions in Decreasing Precedence in awk'
contains the line
expr < expr Less than Numeric None
i believe its a bug. (That hopefully gets fixed by someone who
yet has some experience with the awk codebase.)
| David A. Holland
| dholland at netbsd.org
--steffen
More information about the freebsd-bugs
mailing list