Awk Logical Operators – OR , AND , NOT – POFTUT

# Awk Logical Operators – OR , AND , NOT

Boolean expressions are used to determine given conditions occurrence. Awk provides basic logical operators or `and` , `or` and `not` logic operators. In this tutorial we will look basic usages of these logical operators. Boolen logic operators mainly used with conditional statements where we can get more information from following tutorial.

Awk If, If Else , Else Statement or Conditional Statements

# Example Data

While doing logic examples we will use following test data which file name is `students.txt` . The numbers are exam1, exam2 and exam3

# Awk AND Logic

`and` logic is used to check if all provided Boolean values are `True` . We use  `&&` to specify and logic. The basic usage is providing two Boolean values and we can also provide more than 2 values. The best practice while using `and` logic is surrounding the operators and values with parenthesis `( ... )` . Syntax of and operator is like below.

In this example we will check two exam1 and exam2 grades if they are over `50`we will print the name of the student. Awk AND Logic

As we can see we check columns `2` and `3` which matches with `exam1` and `exam2`

# Awk OR Logic

`OR` logic is used to check given conditions and if one of the condition is true the whole `or` logic will return true. We will use `||` signs to specify OR logic. Syntax of OR logic is like below.

In this example we will check all three exams degrees and find students those have get a degree lower than 50 at least one time. Awk OR Logic

`\$2` , `\$3` and `\$4` matches with `exam1` , `exam2` and `exam3` we check all scores with Or logic if one of them is below 50 . If so we print the student name.

# Awk NOT Logic

NOT logic provides given boolean value reverse. It is simple if given values is `True` after NOT logic return value will be false. If given boolean value is `False` after NOT logic return value will be true. Look following list. We will use `!` sign as NOT operator.

• ! True = False
• ! False = True

Syntax is like below. Using parenthesis will made the script more reliable.

Now time to example we print student names those name is not `ismail` . Awk NOT Logic

Here our conditions is `\$1=="ismail"` and reversed with `!` NOT operator.