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, world
The 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, ... |
Get Unix in a Nutshell, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.