文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>6.4. Formatting Output

6.4. Formatting Output

时间:2006-11-28  来源:h0ng123

6.4. Formatting Output

6.4.1 The print Function

The action part of the awk command is enclosed in curly braces. If no action is specified and a pattern is matched, awk takes the default action, which is to print the lines that are matched to the screen. The print function is used to print simple output that does not require fancy formatting. For more sophisticated formatting, the printf or sprintf functions are used. If you are familiar with C, then you already know how printf and sprintf work.

The print function can also be explicitly used in the action part of awk as {print}. The print function accepts arguments as variables, computed values, or string constants. Strings must be enclosed in double quotes. Commas are used to separate the arguments; if commas are not provided, the arguments are concatenated together. The comma evaluates to the value of the OFS, which is by default a space.

The output of the print function can be redirected or piped to another program, and the output of another program can be piped to awk for printing. (See "Redirection" on page 25 and "Pipes" on page 28.)

Example 6.5.
% date Wed Jul 28 22:23:16 PDT 2004 % date | nawk '{ print "Month: " $2 "\nYear: " , $6 }' Month: Jul Year: 2004 

EXPLANATION

The output of the UNIX date command will be piped to nawk. The string Month: is printed, followed by the second field, the string containing the newline character (\n), and Year:, followed by the sixth field ($6).

Escape Sequences

Escape sequences are represented by a backslash and a letter or number. They can be used in strings to represent tabs, newlines, form feeds, and so forth (see Table 6.1).

Example 6.6.
Tom Jones 4424 5/12/66 543354 Mary Adams 5346 11/4/63 28765 Sally Chang 1654 7/22/54 650000 Billy Black 1683 9/23/44 336500 % nawk '/Sally/{print "\t\tHave a nice day, " $1, $2 "\!"}' employees Have a nice day, Sally Chang! 

Table 6.1. print Escape Sequences

Escape Sequence

Meaning

\b

Backspace

\f

Form feed

\n

Newline

\r

Carriage return

\t

Tab

\047

Octal value 47, a single quote

\c

c represents any other character, e.g., \


EXPLANATION

If the line contains the pattern Sally, the print function prints two tabs, the string Have a nice day, the first (where $1 is Sally) and second fields (where $2 is Chang), followed by a string containing an exclamation mark.

6.4.2 The OFMT Variable

When printing numbers, you may want to control the format of the number. Normally this would be done with the printf function, but the special awk variable, OFMT, can be set to control the printing of numbers when using the print function. It is set by default to %.6g—six significant digits to the right of the decimal are printed. (The following section describes how this value can be changed.)

Example 6.7.
% nawk 'BEGIN{OFMT="%.2f"; print 1.2456789, 12E–2}' 1.25 0.12 

EXPLANATION

The OFMT variable is set so that floating-point numbers (f) will be printed with two numbers following the decimal point. The percent sign (%) indicates a format is being specified.

6.4.3 The printf Function

When printing output, you may want to specify the amount of space between fields so that columns line up neatly. Because the print function with tabs does not always guarantee the desired output, the printf function can be used for formatting fancy output.

The printf function returns a formatted string to standard output, like the printf statement in C. The printf statement consists of a quoted control string that may be embedded with format specifications and modifiers. The control string is followed by a comma and a list of comma-separated expressions that will be formatted according to the specifications stated in the control string. Unlike the print function, printf does not provide a newline. The escape sequence, \n, must be provided if a newline is desired.

For each percent sign and format specifier, there must be a corresponding argument. To print a literal percent sign, two percent signs must be used. See Table 6.2 for a list of printf conversion characters and Table 6.3 for printf modifiers. The format specifiers are preceded by a percent sign; see Table 6.4 for a list of printf format specifiers.

Table 6.2. printf Conversion Characters

Conversion Character

Definition

c

Character

s

String

d

Decimal number

ld

Long decimal number

u

Unsigned decimal number

lu

Long unsigned decimal number

x

Hexadecimal number

lx

Long hexadecimal number

o

Octal number

lo

Long octal number

e

Floating-point number in scientific notation (e-notation)

f

Floating-point number

g

Floating-point number using either e or f conversion, whichever takes the least space


Table 6.3. printf Modifiers

Character

Definition

Left-justification modifier

#

Integers in octal format are displayed with a leading 0; integers in hexadecimal form are displayed with a leading 0x

+

For conversions using d, e, f, and g, integers are displayed with a numeric sign + or –

0

The displayed value is padded with zeros instead of whitespace


Table 6.4. printf Format Specifiers

Format Specifier

What It Does

Given x = 'A', y = 15, z = 2.3, and $1 = Bob Smith:

%c

Prints a single ASCII character.

printf("The character is %c.\n",x)

prints: The character is A.

%d

Prints a decimal number.

printf("The boy is %d years old.\n", y)

prints: The boy is 15 years old.

%e

Prints the e-notation of a number.

printf("z is %e.\n",z)

prints: z is 2.3e+01.

%f

Prints a floating-point number.

printf("z is %f.\n", 2.3 *2)

prints: z is 4.600000.

%o

Prints the octal value of a number.

printf("y is %o.\n", y)

prints: z is 17.

%s

Prints a string of characters.

printf("The name of the culprit is %s.\n", $1)

prints: The name of the culprit is Bob Smith.

%x

Prints the hex value of a number.

printf ("y is %x.\n", y)

prints: x is f.


When an argument is printed, the place where the output is printed is called the field, and the width of the field is the number of characters contained in that field.

The pipe symbol (vertical bar) in the following examples, when part of the printf string, is part of the text and is used to indicate where the formatting begins and ends.

Example 6.8.
1 % echo "UNIX" | nawk ' {printf "|%–15s|\n", $1}' (Output) |UNIX | 2 % echo "UNIX" | nawk '{ printf "|%15s|\n", $1}' (Output) | UNIX| 

EXPLANATION

  1. The output of the echo command, UNIX, is piped to nawk. The printf function contains a control string. The percent sign alerts printf that it will be printing a 15-space, left-justified string enclosed in vertical bars and terminated with a newline. The dash after the percent sign indicates left justification. The control string is followed by a comma and $1. The string UNIX will be formatted according to the format specification in the control string.

  2. The string UNIX is printed in a right-justified, 15-space string, enclosed in vertical bars, and terminated with a newline.

Example 6.9.
% cat employees Tom Jones 4424 5/12/66 543354 Mary Adams 5346 11/4/63 28765 Sally Chang 1654 7/22/54 650000 Billy Black 1683 9/23/44 336500 % nawk '{printf "The name is: %-15s ID is %8d\n", $1, $3}' employees The name is Tom ID is 4424 The name is Mary ID is 5346 The name is Sally ID is 1654 The name is Billy ID is 1683 

EXPLANATION

The string to be printed is enclosed in double quotes. The first format specifier is %–15s. It has a corresponding argument, $1, positioned directly to the right of the comma after the closing quote in the control string. The percent sign indicates a format specification: The dash means left justify, the 15s means 15-space string. At this spot, print a left-justified, 15-space string followed by the string ID is and a number.

The %8d format specifies that the decimal (integer) value of $2 will be printed in its place within the string. The number will be right justified and take up eight spaces. Placing the quoted string and expressions within parentheses is optional.

排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载