Control Letters (The GNU Awk User’s Guide)
5.5.2 Format-Control Letters
A format specifier starts with the character ‘
%’ and ends with a format-control letter—it tells the
printf statement how to output one item. The format-control letter specifies what kind of value to print. The rest of the format specifier is made up of optional modifiers that control how to print the value, such as the field width. Here is a list of the format-control letters:
A floating point number of the form [
0xh.hhhhp+-dd(C99 hexadecimal floating point format). For
%A, uppercase letters are used instead of lowercase ones.
NOTE: The current POSIX standard requires support for
awk. As far as we know, besides
gawk, the only other version of
awkthat actually implements it is BWK
awk. It’s use is thus highly nonportable!
Furthermore, these formats are not available on any system where the underlying C library
printf()function does not support them. As of this writing, among current systems, only OpenVMS is known to not support them.
Print a number as a character; thus, ‘
printf "%c", 65’ outputs the letter ‘
A’. The output for a string value is the first character of the string.
NOTE: The POSIX standard says the first character of a string is printed. In locales with multibyte characters,
gawkattempts to convert the leading bytes of the string into a valid wide character and then to print the multibyte encoding of that character. Similarly, when printing a numeric value,
gawkallows the value to be within the numeric range of values that can be held in a wide character. If the conversion to multibyte encoding fails,
gawkuses the low eight bits of the value as the character to print.
awkversions generally restrict themselves to printing the first byte of a string or to numeric values within the range of a single byte (0–255). (d.c.)
Print a decimal integer. The two control letters are equivalent. (The ‘
%i’ specification is for compatibility with ISO C.)
Print a number in scientific (exponential) notation. For example:
printf "%4.3e\n", 1950
1.950e+03’, with a total of four significant figures, three of which follow the decimal point. (The ‘
4.3’ represents two modifiers, discussed in the next subsection.) ‘
%E’ uses ‘
E’ instead of ‘
e’ in the output.
Print a number in floating-point notation. For example:
printf "%4.3f", 1950
1950.000’, with a minimum of four significant figures, three of which follow the decimal point. (The ‘
4.3’ represents two modifiers, discussed in the next subsection.)
On systems supporting IEEE 754 floating-point format, values representing negative infinity are formatted as ‘
-inf’ or ‘
-infinity’, and positive infinity as ‘
inf’ or ‘
infinity’. The special “not a number” value formats as ‘
-nan’ or ‘
nan’ (see section Other Stuff to Know).
%f’, but the infinity and “not a number” values are spelled using uppercase letters.
%F’ format is a POSIX extension to ISO C; not all systems support it. On those that don’t,
Print a number in either scientific notation or in floating-point notation, whichever uses fewer characters; if the result is printed in scientific notation, ‘
%G’ uses ‘
E’ instead of ‘
Print an unsigned octal integer (see section Octal and Hexadecimal Numbers).
Print a string.
Print an unsigned decimal integer. (This format is of marginal use, because all numbers in
awkare floating point; it is provided primarily for compatibility with C.)
Print an unsigned hexadecimal integer; ‘
%X’ uses the letters ‘
A’ through ‘
F’ instead of ‘
a’ through ‘
f’ (see section Octal and Hexadecimal Numbers).
Print a single ‘
%’. This does not consume an argument and it ignores any modifiers.
NOTE: When using the integer format-control letters for values that are outside the range of the widest C integer type,
gawkswitches to the ‘
%g’ format specifier. If
--lintis provided on the command line (see section Command-Line Options),
gawkwarns about this. Other versions of
awkmay print invalid values or do something else entirely. (d.c.)
NOTE: The IEEE 754 standard for floating-point arithmetic allows for special values that represent “infinity” (positive and negative) and values that are “not a number” (NaN).
Input and output of these values occurs as text strings. This is somewhat problematic for the
awklanguage, which predates the IEEE standard. Further details are provided in Standards Versus Existing Practice; please see there.