manpagez: man pages & more
info gawk
Home | html | info | man

gawk: Comparison Operators Comparison Operators
 "Comparison expressions" compare strings or numbers for relationships
 such as equality.  They are written using "relational operators", which
 are a superset of those in C. ⇒Table 6.3 table-relational-ops.
 describes them.
 Expression         Result
 X '<' Y            True if X is less than Y
 X '<=' Y           True if X is less than or equal to Y
 X '>' Y            True if X is greater than Y
 X '>=' Y           True if X is greater than or equal to Y
 X '==' Y           True if X is equal to Y
 X '!=' Y           True if X is not equal to Y
 X '~' Y            True if the string X matches the regexp denoted by Y
 X '!~' Y           True if the string X does not match the regexp
                    denoted by Y
 SUBSCRIPT 'in'     True if the array ARRAY has an element with the
 ARRAY              subscript SUBSCRIPT
 Table 6.3: Relational operators
    Comparison expressions have the value one if true and zero if false.
 When comparing operands of mixed types, numeric operands are converted
 to strings using the value of 'CONVFMT' (⇒Conversion).
    Strings are compared by comparing the first character of each, then
 the second character of each, and so on.  Thus, '"10"' is less than
 '"9"'.  If there are two strings where one is a prefix of the other, the
 shorter string is less than the longer one.  Thus, '"abc"' is less than
    It is very easy to accidentally mistype the '==' operator and leave
 off one of the '=' characters.  The result is still valid 'awk' code,
 but the program does not do what is intended:
      if (a = b)   # oops! should be a == b
 Unless 'b' happens to be zero or the null string, the 'if' part of the
 test always succeeds.  Because the operators are so similar, this kind
 of error is very difficult to spot when scanning the source code.
    The following list of expressions illustrates the kinds of
 comparisons 'awk' performs, as well as what the result of each
 comparison is:
 '1.5 <= 2.0'
      Numeric comparison (true)
 '"abc" >= "xyz"'
      String comparison (false)
 '1.5 != " +2"'
      String comparison (true)
 '"1e2" < "3"'
      String comparison (true)
 'a = 2; b = "2"'
 'a == b'
      String comparison (true)
 'a = 2; b = " +2"'
 'a == b'
      String comparison (false)
    In this example:
      $ echo 1e2 3 | awk '{ print ($1 < $2) ? "true" : "false" }'
      -| false
 the result is 'false' because both '$1' and '$2' are user input.  They
 are numeric strings--therefore both have the strnum attribute, dictating
 a numeric comparison.  The purpose of the comparison rules and the use
 of numeric strings is to attempt to produce the behavior that is "least
 surprising," while still "doing the right thing."
    String comparisons and regular expression comparisons are very
 different.  For example:
      x == "foo"
 has the value one, or is true if the variable 'x' is precisely 'foo'.
 By contrast:
      x ~ /foo/
 has the value one if 'x' contains 'foo', such as '"Oh, what a fool am
    The righthand operand of the '~' and '!~' operators may be either a
 regexp constant ('/'...'/') or an ordinary expression.  In the latter
 case, the value of the expression as a string is used as a dynamic
 regexp (⇒Regexp Usage; also ⇒Computed Regexps).
    A constant regular expression in slashes by itself is also an
 expression.  '/REGEXP/' is an abbreviation for the following comparison
      $0 ~ /REGEXP/
    One special place where '/foo/' is _not_ an abbreviation for '$0 ~
 /foo/' is when it is the righthand operand of '~' or '!~'.  ⇒Using
 Constant Regexps, where this is discussed in more detail.
© 2000-2018
Individual documents may contain additional copyright information.