DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Using awk

Formatting awk output

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             18
With 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.
Next topic: Variables
Previous topic: Running awk programs

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003