awk's printf statement is the same as that in C,
except that the format specifier is not supported.
The printf statement has the general form:
printf format, expr1, expr2, ..., exprn
where format is a string that contains both information to be printed and specifications on what conversions to perform on the expressions in the argument list, as in the table below. Each specification begins with a ``%'', ends with a letter that determines the conversion, and can include any of the following:
awk printf conversion characters
Character | Prints expression as |
---|---|
%c | single character |
%d | decimal integer |
%e | [-]d.dprecisionE[+-]dd |
%f | [-]ddd.dprecision |
%g | e or f conversion, whichever is shorter, with nonsignificant zeros suppressed |
%o | unsigned octal number |
%s | string |
%x | unsigned hexadecimal number |
%% | print a %; no argument is converted |
printf "%d", 99/2 |
49
|
printf "%e", 99/2 |
4.950000e+01
|
printf "%f", 99/2 |
49.500000
|
printf "%6.2f", 99/2 |
49.50
|
printf "%g", 99/2 |
49.5
|
printf "%o", 99/2 |
61
|
printf "%06o", 99/2 |
000061
|
printf "%x", 99/2 |
31
|
printf "|%s|", "January" |
|January|
|
printf "|%10s|", "January" |
| January|
|
printf "|%-10s|", "January" |
|January |
|
printf "|%.3s|", "January" |
|Jan|
|
printf "|%10.3s|", "January" |
| Jan|
|
printf "|%-10.3s|", "January" |
|Jan |
|
printf "%%" |
%
|