|
|
For more carefully formatted output, awk provides a
C-like printf statement:
printf format, expr1, expr2, ..., exprn
This statement prints each expr according to the corresponding specification in the string format. For example, the following 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, and finally a newline (\n). With input from the file countries, this program prints an aligned table:
CIS 262 Canada 24 China 866 USA 219 Brazil 116 Australia 14 India 637 Argentina 26 Sudan 19 Algeria 18With printf, no output separators or newlines are produced automatically; you must create them yourself by using \n in the format specification. See ``The printf statement'' for a full description of printf.