comparative operator

Last edit

Summary: update from markhobley.yi.org

Changed:

< In awk, the comparative operators can be used to compare strings for lexical equivalence or ordering. The string matching operators are case sensitive and the behaviour of the comparative operators depends on the locale being used. The following code snippet demonstrates how to compare strings for equivalence and lexical ordering:

to

> In awk, the comparative operators can be used to compare strings for lexical equivalence or ordering. The string matching operators are case sensitive and the behaviour of the comparative operators depends on the locale being used.
> The following code snippet demonstrates how to compare strings for equivalence and lexical ordering:

Added:

> See [[numeric strings]].


The comparative operators are used to determine equality or inequality or otherwise make comparisons of numeric or string values, and produce a boolean truth based on the result. A true comparison evaluates to a value of one, whereas a false comparision evaluates to zero.

The following comparative operators are supported by awk:

==[[equality?]]
!=[[inequality?]]
<[[lessthan?]]
>[[greaterthan?]]
<=[[lessthanorequal?]]
>=[[greaterthanorequal?]]
?:[[ternary?]] operator

String comparison

In awk, the comparative operators can be used to compare strings for lexical equivalence or ordering. The string matching operators are case sensitive and the behaviour of the comparative operators depends on the locale being used.

The following code snippet demonstrates how to compare strings for equivalence and lexical ordering:

BEGIN {
  a="BALL"
  b="BELL"
  if (a == b) { print "The strings are equal" }
  if (a != b) { print "The strings are not equal" }
  if (a > b) { print "The first string is lexically after than the second" }
  if (a < b) { print "The first string is lexically before than the second" }
  if (a >= b) { print "The first string is not lexically before than the second" }
  if (a <= b) { print "The first string is not lexically after than the second" }

  # to make a case insensitive comparison convert both strings to the same lettercase:
  a="BALL"
  b="ball"
  if (tolower(a) == tolower(b)) { print "The first and second string are the same disregarding letter case" }
}

Issues with numeric strings

Be aware that making lexical comparisons of strings containing numeric values may cause unexpected results because whether they will be treated as conventional strings or numeric values depends on how the values were obtained, and on which awk interpreter is being used. Numeric strings obtained from the input source, will be treated as numeric values, when compared with other strings containing numeric values. Strings valued defined as constants using doublequote enclosures will be treated as strings of characters and compared lexically. The behaviour of the operators when one value is considered to be numeric (eg from the input source), but the other value has been defined explicitly as a numeric string by using doublequote enclosures may also vary depending on which awk interpreter is being used.

See numeric strings.

The equals sign cannot be used as a comparative operator

In awk, the equals sign cannot be used as a comparative operator. A common programming mistake is to attempt to use the assignment operator as a comparison within an expression:

# This script contains a bug
BEGIN {
  n=2
  IF (n=3) PRINT "n is equal to 3"    # The incorrectly used equals sign will set n to a value of 3
  print n
}