[Top][All Lists]
[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: |
Mahendra Ladhe |
Subject: |
RE: gawk always prints 0 for a hexadecimal number printed using printf %d |
Date: |
Thu, 6 Nov 2008 22:25:18 -0800 |
That's great.
Thanks Sharuzzaman.
Now I can run my script on non-Ubuntu machines as well.
Thanks again,
Mahendra
________________________________
From: Sharuzzaman Ahmat Raslan [mailto:address@hidden
Sent: Friday, November 07, 2008 11:49 AM
To: Mahendra Ladhe
Cc: address@hidden
Subject: Re: gawk always prints 0 for a hexadecimal number printed using
printf %d
Hi Mahendra,
I found some info regarding hex manipulation here:
http://osr507doc.sco.com/cgi-bin/info2html?(gawk.info.gz)Nondecimal%2520
Data&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