
- AWK Tutorial
- AWK - Home
- AWK - Overview
- AWK - Environment
- AWK - Workflow
- AWK - Basic Syntax
- AWK - Basic Examples
- AWK - Built in Variables
- AWK - Operators
- AWK - Regular Expressions
- AWK - Arrays
- AWK - Control Flow
- AWK - Loops
- AWK - Built in Functions
- AWK - User Defined Functions
- AWK - Output Redirection
- AWK - Pretty Printing
- AWK Useful Resources
- AWK - Quick Guide
- AWK - Useful Resources
- AWK - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.