5.3. Formatting Output

5.3.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 easy and 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 output field separator (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 piped to awk for printing. (See "Redirection" on page 22 and "Pipes" on page 25.)

Example 5.8.
% date
						Wed Jan 12 22:23:16 PST 2000

% date | awk '{ print "Month: " $2 "
Year: " , $6 }'
						Month: Jan
						Year: 2000
					

Explanation

The output of the Linux date command will be piped to awk. The string Month: is printed, followed by the second field, the string containing the newline character, , 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 5.2).

Table 5.2. Escape Sequences
Escape SequenceMeaning
Backspace
fForm feed
Newline
Carriage return
Tab
47Octal value 47, a single quote
cc represents any other character, e.g.,

Example 5.9.
							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

% awk '/Sally/{print "		Have 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 two exclamation marks.

5.3.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 5.10.
% awk '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.

5.3.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 imbedded 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, , 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 percentage, two percent signs must be used. See Table 5.3 for a list of conversion characters and Table 5.4 for printf modifiers. The format specifiers are preceded by a percent sign. See Table 5.5 for a list of 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 5.11.
1  % echo "Linux" | awk '{printf "|%–15s|
", $1}'
   (Output)
   |Linux              |

2  % echo "Linux" | awk '{ printf "|%15s|
", $1}'
   (Output)
   |              Linux|
					

Explanation

  1. The output of the echo command, Linux, is piped to awk. 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 Linux will be formatted according to the format specification in the control string.

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

Example 5.12.
% 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

% awk '{printf "The name is: %-15s ID is %8d
", $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.

Table 5.3. Conversion Characters
Conversion CharacterDefinition
cCharacter
sString
dDecimal number
ldLong decimal number
uUnsigned decimal number
luLong unsigned decimal number
xHexadecimal number
lxLong hexidecimal number
oOctal number
loLong octal number
eFloating point number in scientific notation (e-notation)
fFloating point number
gFloating point number using either e or f conversion, whichever takes the least space

Table 5.4. printf Modifiers
CharacterDefinition
-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 –.
0The displayed value is padded with zeros instead of white space.

Table 5.5. Format Specifiers
printf Format SpecifierWhat It Does
Given x = A, y = 15, z = 2.3, and $1 = Bob Smith
%cPrints a single ASCII character.printf("The character is %c ", x) prints: The character is A
%dPrints a decimal number.printf("The boy is %d years old ", y) prints: The boy is 15 years old
%ePrints the e notation of a number.printf("z is %e ", z) prints: z is 2.3e+01
%fPrints a floating point number.printf("z is %f ", 2.3 *2) prints: z is 4.600000
%oPrints the octal value of a number.printf("y is %o ", y) prints: z is 17
%sPrints a string of characters.printf("The name of the culprit is %s ", $1) prints: The name of the culprit is Bob Smith
%xPrints the hex value of a number.printf ("y is %x ", y) prints: x is f

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.117.191