Format and print (2)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.