GROUP patterns follow the syntax:
GROUP exp1 { optional action }
GROUP /re2/ { optional action }
GROUP var3 { optional action }
GROUP "st4" { optional action }
Actions are optional with any particular expression in the GROUP. If no action is given, the next action specified in the group is executed. If no action is specified for the last expression in a group, the default action, {print;} is assigned to it.
Any utility may have more than one GROUP of patterns. A group is
terminated
by any pattern not starting with the GROUP keyword.
GROUP Pattern Advantage
GROUP patterns have two distinct advantages in QTAwk:
For utilities containing many regular expression patterns for which
to
search, a program organized into one or more GROUPs can be many times
faster
than a utility organized as ordinary pattern/action pairs. For example,
the QTAwk utility in ANSI_C
searches
a C source file listing for ANSI C Standard defined names. The utility
organizes
the search into a single GROUP and will search a source file
approximately
6 times faster than the same utility organized as separate
pattern/action
pairs without the use of a GROUP.
GROUP Pattern Disadvantage
GROUP patterns have one disadvantage compared to ordinary pattern/action pairs. QTAwk will find only one of the regular expressions in a GROUP. A set of GROUP patterns:
GROUP expression1 { action1; }
GROUP expression2 { action2; }
GROUP expression3 { action3; }
is similar in execution to:
$0 ~~ expression1 { action1; next; }
$0 ~~ expression2 { action2; next; }
$0 ~~ expression3 { action3; next; }
If more than one regular expression in a group will match a given
string
in the input record, the regular expression listed first in the GROUP
will
be matched and the appropriate action executed. If all regular
expression
patterns in a GROUP must be found in input records, then separate
pattern-action
pairs must be used.
GROUP Pattern Regular Expressions
The regular expressions associated with the GROUP pattern can be any valid QTAwk expression. When the GROUP is first matched against an input record, all expressions in a GROUP are evaluated and converted to regular expressions.
GROUP patterns are converted into an internal form for regular expressions only once, when the pattern is first used to scan an input line. Any variables which are the result of a GROUP expression will be evaluated and converted to a regular expression. Similarly, any named expressions in a regular expression constant, string constant or regular expression value of a variable will be replaced once, at the time of conversion.
If one of the expressions in a GROUP match the current input record, the value of the built-in variable NG is set to the sequence value of the matching expression in the GROUP. Numbering of the expressions in the GROUP starts with 1.
If any expression in a GROUP evaluates to an array, the entire array is utilized for matching at that position of the GROUP, i.e., if any element of the array matches the current input record, the value of NG is assigned the sequence value of the array in the GROUP.