Name
printf
Synopsis
printf[format [, expressions]]Formatted print statement. Expressions or variables can be formatted according to instructions in the format argument. The number of expressions 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 is the total number of digits; m is the number of digits after the decimal point.
- %[-] nc
n specifies minimum field length for format type c. - left-justifies value in field; otherwise, value is right-justified.
Field widths are adjustable. For example, %3.2f limits a floating-point number to a minimum width of three digits, with two digits after the decimal point.
format also can contain embedded escape sequences, \n (newline) and \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, multiple formats should be specified.
Example
Using the script:
{printf ("The sum on line %s is %d.\n", NR, $1+$2)}the following input line:
5 5produces this output, followed by a newline:
The sum on line 1 is 10.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access