Go to the first, previous, next, last section, table of contents.


Specifying Record Ranges with Patterns

A range pattern is made of two patterns separated by a comma, of the form begpat, endpat. It matches ranges of consecutive input records. The first pattern begpat controls where the range begins, and the second one endpat controls where it ends. For example,

awk '$1 == "on", $1 == "off"'

prints every record between `on'/`off' pairs, inclusive.

A range pattern starts out by matching begpat against every input record; when a record matches begpat, the range pattern becomes turned on. The range pattern matches this record. As long as it stays turned on, it automatically matches every input record read. It also matches endpat against every input record; when that succeeds, the range pattern is turned off again for the following record. Now it goes back to checking begpat against each record.

The record that turns on the range pattern and the one that turns it off both match the range pattern. If you don't want to operate on these records, you can write if statements in the rule's action to distinguish them.

It is possible for a pattern to be turned both on and off by the same record, if both conditions are satisfied by that record. Then the action is executed for just that record.


Go to the first, previous, next, last section, table of contents.