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

The printf statement

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:


-
left-justify expression in its field

width
pad field to this width as needed; fields that begin with a leading 0 are padded with zeros

prec
maximum string width or digits to right of decimal point

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

Here are some examples of printf statements with the corresponding output:

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 "%%" %
The default output format of numbers is %.6g; this can be changed by assigning a new value to OFMT. OFMT also controls the conversion of numeric values to strings for concatenation and creation of array subscripts.


Next topic: Output into files
Previous topic: Output separators

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