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

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

Re: gawk always prints 0 for a hexadecimal number printed using printf %


From: Sharuzzaman Ahmat Raslan
Subject: Re: gawk always prints 0 for a hexadecimal number printed using printf %d
Date: Fri, 7 Nov 2008 14:18:35 +0800

Hi Mahendra,

I found some info regarding hex manipulation here:

http://osr507doc.sco.com/cgi-bin/info2html?(gawk.info.gz)Nondecimal%2520Data&lang=en

The possible solution is to use the option --non-decimal-data

Example:

bash-3.00$ gawk --version
GNU Awk 3.1.3
Copyright (C) 1989, 1991-2003 Free Software Foundation.

bash-3.00$ cat file.txt
0xf

bash-3.00$ gawk --non-decimal-data '{ printf "%d\n",$1 }' file.txt
15



On Thu, Nov 6, 2008 at 5:50 PM, Mahendra Ladhe <address@hidden>wrote:

> Hi,
>
>   when a field on a line which is a hexadecimal number(preceded by 0x)
> is printed
>
> using printf with %d format specifier, gawk always prints 0 instead of
> the value of the
>
> field in the decimal format.
>
>
>
>  Kindly see below the interactions at two different Linux machines.
>
>
>
> On Fedora-7 Linux:
>
> ==============
>
> address@hidden:~] gawk --version
>
> GNU Awk 3.1.6
>
> Copyright (C) 1989, 1991-2007 Free Software Foundation.
>
> [snip]
>
>
>
> address@hidden:~] cat /proc/version
>
> Linux version 2.6.21-1.3194.fc7
> (address@hidden) (gcc version 4.1.2
> 20070502 (Red Hat 4.1.2-12)) #1 SMP Wed May 23 22:35:01 EDT 2007
>
> address@hidden:~] cat file.txt
>
> 0xf
>
> address@hidden:~] gawk '{printf("%d\n", $1)}' file.txt
>
> 0
>
> address@hidden:~] gawk '{printf("%d\n", 0xf)}' file.txt
>
> 15
>
>
>
> As seen above, if some field is specified($1, $2 etc), it prints 0
> always, but if a hard coded value like 0xf is
>
> used, the correct value is printed in decimal.
>
>
>
>
>
> On Ubuntu Linux:
>
> ============
>
> From 'man awk' command
>
>
>
> AUTHOR
>
>       Mike Brennan (address@hidden).
>
>
>
> Version 1.2                       Dec 22 1994
> MAWK(1)
>
>
>
> address@hidden:~] cat /proc/version
>
> Linux version 2.6.24-19-generic (address@hidden) (gcc version 4.2.3
> (Ubuntu 4.2.3-2ubuntu7)) #1 SMP Wed Aug 20 22:56:21 UTC 2008
>
>
>
> address@hidden:~] cat file.txt
>
> 0xf
>
> address@hidden:~] awk '{printf("%d\n", $1)}' file.txt
>
> 15
>
>
>
>
>
> So on a Ubuntu Linux with a very old version of (m)wk it's working
> correctly.
>
>
>
>
>
> Thanks,
>
> Mahendra Ladhe
>
>
>
>
>
>


-- 
Sharuzzaman Ahmat Raslan


reply via email to

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