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


Pattern ranges

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

An instruction of the form:
pattern1, pattern2 { action }
performs the given action on every line, starting at an occurrence of pattern1 and ending at the next occurrence of pattern2 (inclusive). For example, the instruction
/Jim/, /Linda/ { print $2 }
prints the second field of all lines between an occurrence of Jim and an occurrence of Linda. Using the hobbies file as our data file, the output is:
reading
bridge
role playing
bridge
When awk finds a record matching pattern2, it begins to look for a line matching pattern1 again. Thus, with this instruction:
/reading/, /role/
the output is
Jim     reading          15     100.00
Jim     bridge            4      10.00
Jim     role playing      5      70.00
Katie   reading          10      60.00
John    role playing      8     100.00
awk prints the first range of records from reading to role and then starts looking for reading again.
awk starts performing the instruction's action as soon as there is a record that matches pattern1. awk does not check to make sure that there is a line matching pattern2 in the rest of the file. This means that:
/Lori/, /Jim/ { print $2 }
begins printing at the first record that contains Lori, and keeps going until it reaches the end of the file. No Jim is found.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014