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