DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with awk

Relational expressions

An awk pattern can be any expression involving comparisons between strings of characters or numbers. awk has six relational operators, and two extended regular expression matching operators, ~ (tilde) and !~, which are discussed in the next section, for making comparisons. ``awk comparison operators'' lists these 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
~ matches
!~ does not match

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 tell what is intended. ``Number or string?'' contains more information about this.) Thus, the pattern $3>100 selects lines where the third field exceeds 100, and the program

   $1 >= "S"
selects lines that begin with the letters S through Z, namely,
   USSR	8650	262	Asia
   USA	3615	219	North America
   Sudan	968	19	Africa
In the absence of any other information, awk treats fields as strings, so the program
   $1 == $4
compares the first and fourth fields as strings of characters, and with the file countries as input, prints the single line for which this test succeeds:
   Australia   2968   14   Australia
If both fields appear to be numbers, the comparisons are done numerically.
Next topic: Extended regular expressions
Previous topic: BEGIN and END

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005