printf
[
format
[
,
expression(s)
]]
Formatted print statement. Fields or variables can be
formatted according to instructions in the
format
argument.
The number of arguments must correspond to the number specified in the
format sections.
format
follows the conventions of the C-language
printf
statement. Here are a few of the most common formats:
-
%s
-
A string.
-
%d
-
A decimal number.
-
%
n
.
m
f
-
A floating point number;
n
= total number of digits.
m
=
number of digits after decimal point.
-
%[-]
nc
-
nc
specifies minimum field length for format type
c
, while
-
justifies value in field; otherwise value is right justified.
format
can also contain embedded escape sequences:
\n
(newline) or
\t
(tab)
being the most common.
Spaces and literal text can be placed in the
format
argument
by quoting the entire argument.
If there are multiple expressions to be printed, there should be
multiple formats specified.
Using the script:
{printf ("The sum on line %s is %d \n", NR, $1+$2)}
The following input line:
5 5
produces this output, followed by a newline:
The sum on line 1 is 10.