AWK - Relational Operators



AWK supports the following relational operators −

Equal to

It is represented by ==. It returns true if both operands are equal, otherwise it returns false. The following example demonstrates this −

Example

awk 'BEGIN { a = 10; b = 10; if (a == b) print "a == b" }'

On executing this code, you get the following result −

Output

a == b

Not Equal to

It is represented by !=. It returns true if both operands are unequal, otherwise it returns false.

Example

[jerry]$ awk 'BEGIN { a = 10; b = 20; if (a != b) print "a != b" }'

On executing this code, you get the following result −

Output

a != b

Less Than

It is represented by <. It returns true if the left-side operand is less than the right-side operand; otherwise it returns false.

Example

[jerry]$ awk 'BEGIN { a = 10; b = 20; if (a < b) print "a  < b" }'

On executing this code, you get the following result −

Output

a < b

Less Than or Equal to

It is represented by <=. It returns true if the left-side operand is less than or equal to the right-side operand; otherwise it returns false.

Example

[jerry]$ awk 'BEGIN { a = 10; b = 10; if (a <= b) print "a <= b" }'

On executing this code, you get the following result −

Output

a <= b

Greater Than

It is represented by >. It returns true if the left-side operand is greater than the right-side operand, otherwise it returns false.

Example

[jerry]$ awk 'BEGIN { a = 10; b = 20; if (b > a ) print "b > a" }'

On executing the above code, you get the following result −

Output

b > a

Greater Than or Equal to

It is represented by >=. It returns true if the left-side operand is greater than or equal to the right-side operand; otherwise it returns false.

b >= a
awk_operators.htm
Advertisements