Name
printf
Synopsis
printf(format[,expr-list]) [dest-expr]
An alternative output statement borrowed from the C language. It has the ability to produce formatted output. It can also be used to output data without automatically producing a newline. format is a string of format specifications and constants. expr-list is a list of arguments corresponding to format specifiers. As for print, output may be redirected to a file or pipe. See printf Formats in printf Formats for a description of allowed format specifiers.
Like any string, 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.
Examples
Using the script:
{ printf("The sum on line %d is %.0f.\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