< Day Day Up > |
6.4. Formatting Output6.4.1 The print FunctionThe 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 SequencesEscape 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!
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 VariableWhen 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 FunctionWhen 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.
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
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. |
< Day Day Up > |