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 |
$3>100This 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 AfricaIn 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 == $4Using the file countries as input, this program prints the single line for which this test succeeds:
Australia 2968 14 AustraliaIf both fields appear to be numbers, awk performs the comparisons numerically.