16.12.2012 Views

z/OS V1R9.0 UNIX System Services Command ... - Christian Grothoff

z/OS V1R9.0 UNIX System Services Command ... - Christian Grothoff

z/OS V1R9.0 UNIX System Services Command ... - Christian Grothoff

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Examples<br />

Output<br />

The print statement prints its arguments with only simple formatting. If it has no<br />

arguments, it prints the entire current input record. awk adds the output record<br />

separator ORS to the end of the output that each print statement produces; when<br />

commas separate arguments in the print statement, the output field separator OFS<br />

separates the corresponding output values. ORS and OFS are built-in variables,<br />

whose values you can change by assigning them strings. The default output record<br />

separator is a newline, and the default output field separator is a space.<br />

The variable OFMT gives the format of floating-point numbers output by print. By<br />

default, the value is %.6g; you can change this by assigning OFMT a different string<br />

value. OFMT applies only to floating-point numbers (ones with fractional parts).<br />

The printf statement formats its arguments using the fmt argument. Formatting is<br />

the same as for the built-in function sprintf. Unlike print, printf does not add output<br />

separators automatically. This gives the program more precise control of the output.<br />

The print and printf statements write to stdout. You can redirect output to a file or<br />

pipe.<br />

If you add >expr to a print or printf statement, awk treats the string value of expr<br />

as a filename, and writes output to that file. Similarly, if you add >>expr, awk sends<br />

output to the current contents of the file. The distinction between > and >> is<br />

important only for the first print to the file expr. Subsequent outputs to an already<br />

open file append to what is there already.<br />

You cannot use such ambiguous statements as:<br />

print a > b c<br />

Use parentheses to resolve the ambiguity.<br />

If you add |expr to a print or printf statement, awk treats the string value of expr<br />

as an executable command and runs it with the output from the statement piped as<br />

input into the command.<br />

As mentioned earlier, you can have only a limited number of files and pipes open at<br />

any time. To avoid going over the limit, use the close function to close files and<br />

pipes when you no longer need them.<br />

print and printf are also available as functions with the same calling sequence, but<br />

no redirection.<br />

1. The following example:<br />

awk ’{print NR ":" $0}’ input1<br />

outputs the contents of the file input1 with line numbers prepended to each<br />

line.<br />

2. The following is an example using var=value on the command line:<br />

awk ’{print NR SEP $0}’ SEP=":" input1<br />

awk can also read the program script from a file as in the command line:<br />

awk –f addline.awk input1<br />

which produces the same output when the file addline.awk contains:<br />

awk<br />

Chapter 2. Shell command descriptions 43

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!