Format and print (2)
The printf statement specifies the width and alignment of the output field. A format expression is composed of three optional modifiers, following "%" and appearing before the format specifier.
%-Width. precision format-specifier
Width that describes the width of the output field is a numerical value. When the field width is specified, the content of this field is aligned to the right by default. You must specify "-" to set the left alignment. Therefore, the output of "%-20 s" is a string with a domain length of 20 characters to the left alignment. If the string is less than 20 characters, the field will be filled with spaces. In the following example, a "1" is output to indicate the actual length of the output domain. The first example is the right-aligned text:
Printf ("| % 10s | \ n", "hello ")
The result is:
| Hello |
The following example shows the left-aligned text:
Printf ("| %-10s | \ n", "hello ")
The result is:
| Hello |
The precision modifier is used in decimal or floating-point numbers to control the number of digits on the right of the decimal point. For a string value, it is used to control the maximum number of characters to print. Note that the default precision value of a value is "%. 6g ".
You can dynamically specify the width and precision based on the values in the print or printf parameter list. You can use the actual asterisk value to implement this function:
Printf ("% *. * g \ n", 5, 3, myvar );
In this example, the width is 5 and the precision is 3. the value to be printed comes from myvar.
The default precision of the print statement output value can be changed by setting the system variable OFMT. For example, if you use awk to print a report that contains a dollar ($) value, you can set OFMT to "%. 2f ".
The complete syntax of the format expression can solve the problem of alignment of fields and titles in filesum. One reason we output the file size before the file name is that the output fields are more likely to be aligned in this order, and they can be aligned to a large extent. The solution provided by printf allows us to set the width of the output field. Therefore, each field starts in the same column.
Let's re-adjust the output field of the filesum report. We want to get the smallest field width so that the second field starts at the same position. The domain width should be placed between % and the conversion specifier. The "%-15 s" field is 15 characters in width and left-aligned. "% 10d" has no hyphens, and is right aligned. This is the representation of the expected decimal value.
Printf ("%-15s \ t % 10d \ n", $9, $5)
This generates a report where the data is aligned by column and the numbers are right aligned. Let's take a look at how the printf statement is applied in the END:
Printf ("Total: % d bytes (% d files) \ n", sum, filenum)
The title in the BEGIN rule is also changed as appropriate. Use the printf statement. Filesum generates the following results:
$./Filesum g *
FILE BYTES
G 23
Gawk, 2237
Gawk. mail 1171
Gawk. test 74
Gawkro 1, 264
Gfilesum 610
Grades 64
Grades. awk 231
Grepscript 6
Total: 4680 bytes (9 files)