z/OS UNIX System Services User's Guide
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Placeholders

z/OS UNIX System Services User's Guide
SA23-2279-00

The form of the placeholder %5s tells awk how to print the associated value. All placeholders begin with % and end in a letter. The following are some of the most common letters used in placeholders:
c
If the associated value is an integer, printf prints the character in the native character set that has that integer value; if the associated value is a string, printf prints the first character of the string.
d
An integer in decimal form (base 10).
e
A floating-point number in scientific notation, as in -d.ddd dddE+dd.
f
A floating-point number in conventional form, as in -ddd.ddd ddd.
g
A floating-point number in either e or f form, whichever is shorter; also, nonsignificant zeros are not printed.
o
An unsigned integer in octal form (base 8).
s
A string.
x
An unsigned integer in hexadecimal form (base 16).
For example, the format string:
"%s %d\n"
contains two placeholders: %s represents a string, and %d represents a decimal integer.
Between the % and the letter at the end of the placeholder, you can put additional information. If you put an integer, as in %5s, the number is used as a width. awk prints the corresponding value using (at least) the given number of characters. Therefore in:
$2 == "bridge" { printf "%5s plays bridge\n", $1 }
the value of the string $1 replaces the placeholder %5s and is always printed using five characters. The output is therefore:
  Jim plays bridge
Linda plays bridge
 Lori plays bridge
as shown before. If you just write:
$2 == "bridge" { printf "%s plays bridge\n", $1 }
without the 5, the output is:
Jim plays bridge
Linda plays bridge
Lori plays bridge
If no width is given, awk prints values using the smallest number of characters possible.
awk also lets you put a minus sign () in front of the number in the width position. The amount of output space is the same, but the information is left-justified. For example:
$2 == "bridge" { printf "%–5s plays bridge\n", $1 }
prints:
Jim   plays bridge
Linda plays bridge
Lori  plays bridge
A placeholder for a floating-point number can also contain a precision. You can write this as a dot (decimal point) followed by an integer. Specifying a precision tells printf how many digits to print after the decimal point in a floating-point number. For example, in:
$1 == "John" { printf "$%.2f on %s\n", $4 * 1.05, $2 }
the placeholder %.2f indicates that printf is to print all floating-point numbers with two digits after the decimal point. The output of this program is:
$105.00 on role playing
$31.50 on jogging
For good-looking output, you might specify both a width and a precision. For example, the program:
$1 == "John" { printf "$%6.2f on %s\n", $4 * 1.05, $2 }
prints the following:
$105.00 on role playing
$ 31.50 on jogging
%6.2f indicates that the corresponding floating-point value should be printed with a width of six characters, with two characters after the decimal point.
Here are a few more awk programs that work on the hobbies file. Predict what each prints and run them to see if your prediction is right:
(a)  { printf "%6s %s\n", $1, $2 }
(b)  { printf "%20s: %2d hours/week\n", $2, $3 }
(c)  $1=="Katie" { printf "%20s: $%6.2f\n",$2,$4 }

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014