21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Chapter 13: Practical awk Programs 241The BEGIN rule simply sets RS to the empty string, so that awk splits records at blanklines (see Section 3.1 [How Input Is Split into Records], page 36). It sets MAXLINES to 100,since 100 is the maximum number of lines on the page (20 * 5 = 100).Most of the work is done in the printpage function. The label lines are stored sequentiallyin the line array. But they have to print horizontally; line[1] next to line[6],line[2] next to line[7], and so on. Two loops are used to accomplish this. The outerloop, controlled by i, steps through every 10 lines of data; this is each row of labels. Theinner loop, controlled by j, goes through the lines within the row. As j goes from 0 to 4,‘i+j’ is the j-th line in the row, and ‘i+j+5’ is the entry next to it. The output ends uplooking something like this:line 1 line 6line 2 line 7line 3 line 8line 4 line 9line 5 line 10...As a final note, an extra blank line is printed at lines 21 and 61, to keep the outputlined up on the labels. This is dependent on the particular brand of labels in use when theprogram was written. You will also note that there are 2 blank lines at the top and 2 blanklines at the bottom.The END rule arranges to flush the final page of labels; there may not have been an evenmultiple of 20 labels in the data:# labels.awk --- print mailing labels# Each label is 5 lines of data that may have blank lines.# The label sheets have 2 blank lines at the top and 2 at# the bottom.BEGIN { RS = "" ; MAXLINES = 100 }function printpage( i, j){if (Nlines

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

Saved successfully!

Ooh no, something went wrong!