Information Technology Reference
In-Depth Information
as
\\
.
R3:
Intervals of the type
a-z
,
A-Z
,
0-9
in
range
are permitted. For example,
1-9
.
R4:
The hyphen
-
must be at the beginning or the end of
range
in order to
be recognized as itself.
R5:
The carat
^
must not be the first character in
range
in order to be recog-
nized as itself.
(6)
[^
range
]
matches any character not in
range
.Therules
R1
-
R4
set
under 5) also apply here.
(7)
pattern
?
stands for 0 or 1 copies of
pattern
where
pattern
is a specific
character, the period
.
(meaning any character) or a range
[
...
]
as described
under 5) and 6) or something in parentheses.
pattern
*
stands for 0 or any
number of concatenated copies of
pattern
.
pattern
+
stands for 1 or any number
of concatenated copies of
pattern
.
(8) The ordinary parentheses
(
and
)
are used for grouping.
(9) The vertical slash
|
is used to define alternatives.
A.2. Advanced Patterns in
awk
Address patterns in
awk
that select the pattern space for action can be
(1) regular expressions as described in A.1,
(2) algebraic-computational expressions involving variables
20
and func-
tions, and
(3) Boolean combinations of anything listed under 1) or 2).
Essentially, everything can be combined in a sensible way to customize a
pattern.
Example:
In the introduction, the following is used:
awk '(($1~/^between$/)||($(NF)~/^between$/))&&($0~/ through /)' -
This prints every line of input where the first or (
||
) last field equals
between
and (
&&
) there exits a field that equals
through
on the line by invoking the default
action (
i.e.
, printing). It is assumed that fields are separated by blanks. This is used
in the very first example code in the introduction.
20
For example, the variable fields of the input record can be matched against pat-
terns using the tilde operator.