printf Formats
Format specifiers for printf and sprintf have the following form:
%[posn$][flag][width][.precision]letter
The control letter is required. The format conversion control letters are given in the following table.
|
Character |
Description |
|
|
ASCII character. |
|
|
Decimal integer. |
|
|
Decimal integer. (Added in POSIX) |
|
|
Floating-point format
([-]d.precision
|
|
|
Floating-point format
([-]d.precision
|
|
|
Floating-point format ([-]ddd.precision). |
|
|
|
|
|
|
|
|
Unsigned octal value. |
|
|
String. |
|
|
Unsigned decimal value. |
|
|
Unsigned hexadecimal number. Uses
|
|
|
Unsigned hexadecimal number. Uses
|
|
|
Literal |
gawk allows you to provide
a positional specifier after the % (posn$). A positional specifier is an
integer count followed by a $.
The count indicates which argument to use at that point. Counts
start at one, and don’t include the format string. This feature is
primarily for use in producing translations of format strings. For
example:
$ gawk 'BEGIN { printf "%2$s, %1$s\n", "world", "hello" }'
hello, worldThe optional flag is one of the following:
|
Character |
Description |
|
|
Left-justify the formatted value within the field. |
|
space |
Prefix positive values with a space and negative values with a minus. |
|
|
Always prefix numeric values with a sign, ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access