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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

awk<br />

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

3. The following program appends all input lines starting with January to the file<br />

jan (which may or may not exist already), and all lines starting with February<br />

or March to the file febmar:<br />

/^January/ {print >> "jan"}<br />

/^February|^March/ {print >> "febmar"}<br />

4. This program prints the total and average for the last column of each input<br />

line:<br />

{s += $NF}<br />

END {print "sum is", s, "average is", s/NR}<br />

5. The next program interchanges the first and second fields of input lines:<br />

{<br />

}<br />

tmp = $1<br />

$1 = $2<br />

$2 = tmp<br />

print<br />

6. The following inserts line numbers so that output lines are left-aligned:<br />

{printf "%–6d: %s\n", NR, $0}<br />

7. The following prints input records in reverse order (assuming sufficient<br />

memory):<br />

{<br />

}<br />

END {<br />

}<br />

a[NR] = $0 # index using record number<br />

for (i = NR; i>0; --i)<br />

print a[i]<br />

8. The following program determines the number of lines starting with the same<br />

first field:<br />

{<br />

++a[$1] # array indexed using the first field<br />

}<br />

END { # note output will be in undefined order<br />

for (i in a)<br />

print a[i], "lines start with", i<br />

}<br />

You can use the following program to determine the number of lines in each<br />

input file:<br />

{<br />

}<br />

END {<br />

}<br />

++a[FILENAME]<br />

for (file in a)<br />

if (a[file] == 1)<br />

print file, "has 1 line"<br />

else<br />

print file, "has", a[file], "lines"<br />

9. The following program illustrates how you can use a two-dimensional array in<br />

awk. Assume the first field of each input record contains a product number, the<br />

second field contains a month number, and the third field contains a quantity<br />

(bought, sold, or whatever). The program generates a table of products versus<br />

month.<br />

BEGIN {NUMPROD = 5}<br />

{<br />

array[$1,$2] += $3<br />

}<br />

44 z/<strong>OS</strong> <strong>V1R9.0</strong> <strong>UNIX</strong> <strong>System</strong> <strong>Services</strong> <strong>Command</strong> Reference

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

Saved successfully!

Ooh no, something went wrong!