Format printing (i)

Source: Internet
Author: User

Many of the scripts we write can do a good job of manipulating the data, but the output is not formatted appropriately. This is because the basic print statement can do a limited amount of work. Because most of the functionality of awk is to produce reports, it is important to produce formatted reports in a neat style. The program Filesum can handle the data well, but its report lacks a neat format.

The printf provided by awk can replace the print statement, and printf borrows the C programming language. The printf statement can print a simple string as well as the print statement.

Awk' BEGIN {printf ("Hello, world\n")} '

As you can see first, the main difference between printf and print is that printf does not provide the automatic line wrapping feature. You must explicitly specify "\ n" for it.

The complete syntax for a printf statement consists of two parts:

Printf( forMat-Expression[,Arguments])

The parentheses are optional. The first part is an expression that describes the format, usually in the form of a string constant enclosed in quotation marks. The second part is a list of parameters, such as the variable list, which corresponds to the format description. Before the format description, there is a percent sign (%), and the format specifier is one of the characters listed in the following table. The two main format specifiers are S and D, s represent strings, and d represent decimal integers

format specifier
function
%c Print a single ASCII character
printf (" The character is%c\n ", x)
Output: The character is A
%d print a decimal number
printf (" The boy is%d years old\n ", y)
Output: The boy is a years old
%e The E-notation form of printed numbers
printf (" Z is%e\n ", z) Printing: Z is 2.3e+0 1
%f print a floating-point number
printf (" Z is%f\n ", 2.3 * 2)
output: Z is 4.600000
%o octal
printf for printing numbers (" Y is%o\n ", y)
output: Z is x
%s print a string
print (" The name of the culprit is%s\n ", $)
Output: The name of the culprit is Bob smith< /td>
%x hexadecimal value of printed digits
printf (" Y is%x\n ", y)
Output: X is f

The following example uses printf to produce an output in rule 2 of the program FileNum. It outputs a string and a decimal value on a different two fields:

Printf("%d\t%s\n", $,$9)

The statement outputs a value of $ $, followed by a tab \ T and $9, and then outputs a newline character (\ n). A corresponding parameter must be provided for each format description.

Reference: http://www.linuxawk.com/communication/526.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Format printing (i)

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.