This topic is about AWK – Logical Operators.
AWK supports the following logical operators −
Logical AND
It is represented by &&. Its syntax is as follows −
Syntax
expr1 && expr2
It evaluates to true if both expr1 and expr2 evaluate to true; otherwise it returns false. expr2 is evaluated if and only if expr1 evaluates to true. For instance, the following example checks whether the given single digit number is in octal format or not.
Example
[jerry]$ awk 'BEGIN { num = 5; if (num >= 0 && num <= 7) printf "%d is in octal format\n", num }'
On executing this code, you get the following result −
Output
5 is in octal format
Logical OR
It is represented by ||. The syntax of Logical OR is −
Syntax
expr1 || expr2
It evaluates to true if either expr1 or expr2 evaluates to true; otherwise it returns false. expr2 is evaluated if and only if expr1 evaluates to false. The following example demonstrates this −
Example
[jerry]$ awk 'BEGIN { ch = "\n"; if (ch == " " || ch == "\t" || ch == "\n") print "Current character is whitespace." }'
On executing this code, you get the following result −
Output
Current character is whitespace
Logical NOT
It is represented by exclamation mark (!). The following example demonstrates this −
Example
! expr1
It returns the logical compliment of expr1. If expr1 evaluates to true, it returns 0; otherwise it returns 1. For instance, the following example checks whether a string is empty or not.
Example
[jerry]$ awk 'BEGIN { name = ""; if (! length(name)) print "name is empty string." }'
On executing this code, you get the following result −
Output
name is empty string.
In this topic we learned about AWK – Logical Operators. To know more, Click Here.
Pingback: AWK - Operators - Adglob Infosystem Pvt Ltd
Pingback: AWK - Relational Operators - Adglob Infosystem Pvt Ltd