Open GoogleCodeExporter opened 8 years ago
mawk is not the only one having problems with this.
The same problem happens with gawk.
busybox awk seems to do the thing you expect.
For gawk there are two solutions:
- strtonum
- --non-decimal-data
https://www.gnu.org/software/gawk/manual/html_node/Nondecimal_002dnumbers.html
$ for AWK in mawk gawk 'busybox awk' ; do
echo "$AWK:";
${AWK} '{ printf "%c == %c\n", $1, $2 }' <<<$'0x41 65\n0x2d 45';
echo;
done
mawk:
A == A
0 == -
gawk:
0 == A
0 == -
busybox awk:
A == A
- == -
$ for AWK in mawk gawk 'busybox awk' ; do
echo "$AWK:";
${AWK} '{ printf "%c == %c\n", $1+0, $2 }' <<<$'0x41 65\n0x2d 45';
echo;
done
mawk:
A == A
- == -
gawk:
== A
== -
busybox awk:
A == A
- == -
$ for AWK in mawk gawk 'busybox awk' ; do
echo "$AWK:";
${AWK} '{ printf "%c == %c\n", int($1), $2 }' <<<$'0x41 65\n0x2d 45';
echo;
done
mawk:
A == A
- == -
gawk:
== A
== -
busybox awk:
A == A
- == -
$ for AWK in mawk gawk 'busybox awk' ; do
echo "$AWK:";
${AWK} '{ printf "%c == %c\n", strtonum($1), $2 }' <<<$'0x41 65\n0x2d 45';
echo;
done
mawk:
mawk: line 2: function strtonum never defined
gawk:
A == A
- == -
busybox awk:
awk: cmd. line:1: Call to undefined function
for AWK in mawk gawk 'busybox awk' ; do
echo "$AWK:";
${AWK} --non-decimal-data '{ printf "%c == %c\n", $1, $2 }' <<<$'0x41 65\n0x2d 45';
echo;
done
mawk:
mawk: not an option: --non-decimal-data
gawk:
A == A
- == -
busybox awk:
awk: unrecognized option `--non-decimal-data'
BusyBox v1.15.1 (2013-10-21 09:15:29 EDT) multi-call binary
Usage: awk [OPTIONS] [AWK_PROGRAM] [FILE]...
Options:
-v VAR=VAL Set variable
-F SEP Use SEP as field separator
-f FILE Read program from file
Original comment by hulselma...@gmail.com
on 16 Jul 2015 at 6:45
Thanks for that - good to know there are workarounds, even though there's no
single one that works for all versions (using `int()` comes closest, but Gawk
still needs `--non-decimal-data` or `--posix` in addition).
BSD Awk also works as expected.
Gawk's default behavior of not recognizing hex. numbers is by design, and can
be changed as you describe (`--posix` would also make it work, but implies many
additional behavioral changes).
The POSIX spec. says that compliant implementations are allowed, but not
required to accept hex. numbers.
However, the fact that it *half* works in Mawk makes me suspect that it's a bug.
Original comment by mkleme...@gmail.com
on 16 Jul 2015 at 7:08
Original issue reported on code.google.com by
mkleme...@gmail.com
on 14 May 2015 at 12:07