Subject: Re: awk problem
To: Shin'ichiro TAYA <taya@ba2.so-net.ne.jp>
From: Frederick Bruckman <fredb@immanent.net>
List: current-users
Date: 03/25/2004 08:53:29
On Thu, 25 Mar 2004, Shin'ichiro TAYA wrote:

> In the script above, awk uses following expression.
>
>    awk 'BEGIN { printf "\x00\x52\x50\x4f" }
>
> It seems that nawk doesn't support this but gawk does.
>
>    % awk 'BEGIN { printf "\x00\x52\x50\x4f" }' | hexdump -C
>    %
>
>    % gawk 'BEGIN { printf "\x00\x52\x50\x4f" }' | hexdump -C
>    00000000  00 52 50 4f                                       |.RPO|
>    00000004
>    %
>
> Is this a problem of nawk ?
> Or is this a GNU awk specific extention?

Actually, the hex escapes are a non-POSIX extension, but it does work
as long as you leave out the "\x00". POSIX says the behaviour is
undefined if you try to output an ASCII NUL via a backslash escape
followed by all zeroes (e.g. "\000"). POSIX doesn't have anything to
say about "\x" escapes. Gawk does what you seem to expect anyhow, but
nawk does not.

To get a NUL in the output stream, you could use a placeholder, then
transform it using "${TR}". E.g.:

awk 'BEGIN { printf "\177\122\120\117" }' \
| tr '\177' '\000' | hexdump -C
00000000  00 52 50 4f                                       |.RPO|
00000004


Frederick