%a 转换说明符无法识别

%a conversion specifier not recognized

POSIX Awk 说:

The printf statement shall produce output based on a notation similar to the File Format Notation used to describe file formats in this volume of POSIX.1-2008 (see XBD File Format Notation).

File Format Notation定义%a:

The floating-point number argument representing a floating-point number shall be converted in the style "[-]0xh.hhhhp±d" [...]

但是 Gawk 和 Mawk 都不支持这个:

$ gawk 'BEGIN {printf "%a", 1}'
%a

$ mawk 'BEGIN {printf "%a", 1}'
mawk: run time error: improper conversion(number 1) in printf("%a")

这是为什么?

您正在查看 POSIX 2008 SUSv4 (Single Unix Specification) 文档。许多软件早于此,包括 gawk。我怀疑 gawk 实现是 2001 SUSv2,并且没有随时间完全更新。 Linux (glibc) printf(3) man page 暗示了这个问题(请参阅 %a 的描述大约一半,抱歉没有指向 link 的锚点):

a, A

(C99; not in SUSv2) For a conversion, the double argument is converted to hexadecimal notation (using the letters abcdef) in the style [-]0xh.hhhhp±; [...]

nawk/mawk/gawk 不要简单地逐字调用底层 libc 的 printf()sprintf(),它们或多或少地重新实现了格式字符串加工。对于 mawk,请参阅 do_printf() 例如功能。 Perl 也实现了它自己的格式处理,sprintf man page 前面有更多细节。

支持%a/%A的事情是:

  • glibc-2.1 及更高版本 (1997),C99 features
  • 需要它
  • coreutils-5.1(ish) printf(即 /usr/bin/printf
  • bash 的内置 printf,因为 bash-2.05 如果 libc 支持
  • perl 的 sprintf 自 perl-5.22.0

在此处查看已接受的答案以了解更多背景信息:The format specifier %a for printf() in C