gawk: Control Letters
1
1 5.5.2 Format-Control Letters
1 ----------------------------
1
1 A format specifier starts with the character '%' and ends with a
1 "format-control letter"--it tells the 'printf' statement how to output
1 one item. The format-control letter specifies what _kind_ of value to
1 print. The rest of the format specifier is made up of optional
1 "modifiers" that control _how_ to print the value, such as the field
1 width. Here is a list of the format-control letters:
1
1 '%c'
1 Print a number as a character; thus, 'printf "%c", 65' outputs the
1 letter 'A'. The output for a string value is the first character
1 of the string.
1
1 NOTE: The POSIX standard says the first character of a string
1 is printed. In locales with multibyte characters, 'gawk'
1 attempts to convert the leading bytes of the string into a
1 valid wide character and then to print the multibyte encoding
1 of that character. Similarly, when printing a numeric value,
1 'gawk' allows the value to be within the numeric range of
1 values that can be held in a wide character. If the
1 conversion to multibyte encoding fails, 'gawk' uses the low
1 eight bits of the value as the character to print.
1
1 Other 'awk' versions generally restrict themselves to printing
1 the first byte of a string or to numeric values within the
1 range of a single byte (0-255). (d.c.)
1
1 '%d', '%i'
1 Print a decimal integer. The two control letters are equivalent.
1 (The '%i' specification is for compatibility with ISO C.)
1
1 '%e', '%E'
1 Print a number in scientific (exponential) notation. For example:
1
1 printf "%4.3e\n", 1950
1
1 prints '1.950e+03', with a total of four significant figures, three
1 of which follow the decimal point. (The '4.3' represents two
1 modifiers, discussed in the next node.) '%E' uses 'E' instead of
1 'e' in the output.
1
1 '%f'
1 Print a number in floating-point notation. For example:
1
1 printf "%4.3f", 1950
1
1 prints '1950.000', with a total of four significant figures, three
1 of which follow the decimal point. (The '4.3' represents two
1 modifiers, discussed in the next node.)
1
1 On systems supporting IEEE 754 floating-point format, values
1 representing negative infinity are formatted as '-inf' or
1 '-infinity', and positive infinity as 'inf' or 'infinity'. The
11 special "not a number" value formats as '-nan' or 'nan' (⇒Math
Definitions).
1
1 '%F'
1 Like '%f', but the infinity and "not a number" values are spelled
1 using uppercase letters.
1
1 The '%F' format is a POSIX extension to ISO C; not all systems
1 support it. On those that don't, 'gawk' uses '%f' instead.
1
1 '%g', '%G'
1 Print a number in either scientific notation or in floating-point
1 notation, whichever uses fewer characters; if the result is printed
1 in scientific notation, '%G' uses 'E' instead of 'e'.
1
1 '%o'
1 Print an unsigned octal integer (⇒Nondecimal-numbers).
1
1 '%s'
1 Print a string.
1
1 '%u'
1 Print an unsigned decimal integer. (This format is of marginal
1 use, because all numbers in 'awk' are floating point; it is
1 provided primarily for compatibility with C.)
1
1 '%x', '%X'
1 Print an unsigned hexadecimal integer; '%X' uses the letters 'A'
11 through 'F' instead of 'a' through 'f' (⇒
Nondecimal-numbers).
1
1 '%%'
1 Print a single '%'. This does not consume an argument and it
1 ignores any modifiers.
1
1 NOTE: When using the integer format-control letters for values that
1 are outside the range of the widest C integer type, 'gawk' switches
1 to the '%g' format specifier. If '--lint' is provided on the
1 command line (⇒Options), 'gawk' warns about this. Other
1 versions of 'awk' may print invalid values or do something else
1 entirely. (d.c.)
1