|
|
For more carefully formatted output, awk provides a C-like printf statement
printf format, expr[1], expr[2], . . ., expr[n]which prints the expr[i]'s according to the specification in the string format. For example, the awk program
{ printf "%10s %6d\n", $1, $3 }
prints
the first field (``$1'')
as a string of 10 characters (right-justified),
then a space,
then the third field (``$3'')
as a decimal number in a six-character field,
then a newline (\n).
With input from the file
countries,
this program
prints an aligned table:
USSR 262
Canada 24
China 866
USA 219
Brazil 116
Australia 14
India 637
Argentina 26
Sudan 19
Algeria 18
With printf, no output separators or newlines are produced automatically; you must create them yourself by using \n in the format specification. ``The printf statement'' contains a full description of printf.