Previous topic |
Next topic |
Contents |
Contact z/OS |
Library |
PDF
Formatting the output z/OS UNIX System Services User's Guide SA23-2279-00 |
|
The output of the program:
is:
This output shows the amount of money per week that Jim spent
on his hobbies. However, money amounts usually have only two digits
after the decimal point. How can you change the program to make the
money amounts appear more normal? The answer is to use the printf action
instead of print. This lets you specify the format in which awk prints the output.A printf action looks like this:
The format-string indicates the output
format. The values are the data to be printed.A format string contains two kinds of items:
As an example, try running the following program on the hobbies file:
awk prints:
The format string:
has one placeholder: %5s. When printf prints its output, replacing the placeholder with the value $1, which is the first field of the record being examined. The
rest of the format string is just printed out as is.
Note: The
format string ends in \n; for more information, see Escape sequences.
|
Copyright IBM Corporation 1990, 2014
|