LINUX AWK Tutorial Series | Chapter 6

Hello Guys.. Welcome

In this series, we are in chapter 6. Now I am going to discuss the conditional statements in AWK.


Now, what are these conditional statements? 

Conditional statements help to process a set of activities based on a certain condition which is defined.

The type of conditional statements which we see across the most programming language are here as well

  • if
  • else
  • else if
Sample syntax of if else

if (conditon true)
then actions;
else if (condition1 true)
then actions;
else
actions


Now let's see how to use them in AWK.


Example 1:

Now let's say I want to give a 10% appraisal to all employees having a salary of less than 30000.
I am going to use my Employee data csv file.




If you understand the previous chapters it would be easy to find what each part is doing. The only addition here is that I have put if (condition) and based on that printing my data.

Try at Home: Try to do the same with AWK  scripts


Example 2:

Now let's say I want to give a 10% appraisal to all employees having a salary of less than 30000 and 5% to all having greater than that.

Here I can make use of if/else blocks.




if and else will get separated by a semicolon(;) in between.


Try at Home: Try to do the same with AWK  scripts

Example 3:

Adding one more condition

 I want to give a 10% appraisal to all employees having a salary of less than 30000 and 5% to all having greater than that and 1 percent to all having more than 200000.

Now here I can make use of if, else if, and else. All three at one time




so I am checking that in 3 parts,
1) salary of less than 30000
2) salary of more than 200000
3) all other salaries

More chapters to continue.

If you like please follow and comment