Relational operators
An awk pattern can be any expression involving comparisons
between strings of characters or numbers. To make comparisons,
awk includes six relational operators and two regular
expression matching operators, ~ (tilde) and !~, (discussed in
``Regular expressions'').
``awk comparison operators''
shows the relational operators and their meanings.
awk comparison operators
Operator
|
Meaning
|
<
|
less than
|
<=
|
less than or equal to
|
==
|
equal to
|
!=
|
not equal to
|
>=
|
greater than or equal to
|
>
|
greater than
|
In a comparison, if both operands are numeric, a numeric comparison
is made; otherwise, the operands are compared as strings. (Every
value might be either a number or a string; usually awk
can determine what is intended. See
``Number or string?''
for more information about this.) Thus, the following pattern
selects lines where the third field exceeds 100:
$3>100
This program selects lines that begin with the letters ``S'' through
``Z'' (that is, lines with an ASCII value greater than or
equal to ``S''):
$1 >= "S"
The output looks like this:
USA 3615 219 North America
Sudan 968 19 Africa
USA 3615 219 North America
Sudan 968 19 Africa
In the absence of any other information, awk treats fields
as strings. Thus, the following program compares the first and
fourth fields as strings of characters:
$1 == $4
Using the file countries as input, this program prints the
single line for which this test succeeds:
Australia 2968 14 Australia
If both fields appear to be numbers, awk performs the
comparisons numerically.
Next topic:
Regular expressions
Previous topic:
BEGIN and END
© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003