Awk is popular text processing tools widely used in Linux/Unix operating system, its executable is also available for other operating system as well.
Previously we wrote some post on awk in which tried to explain various awk features, in this post we will explain decision making code with if else statements in awk.

Syntax

Before get into some example of awk if statements, we should first try to see syntax like every language has its own syntax. So awk if statements is like below

awk '{
	if(condition)
     {
	statements
	code
     }
}	


We can expand same kind of code with nested conditions to if else or further complex conditions.

Awk if statements

While coding, we have many conditions that need to true before applying some code of provided data on it. So we have some kind of concept in awk which has provision to check conditions before deploy further code on provided data.

Let’s see some example for it, like below

ssirohi@jarvis:~$ awk -F: '{if($1=="root") {print $0}}' /etc/passwd   
root:x:0:0:root:/root:/usr/bin/zsh                                    

In above condition we print statement only if first column match certain string. We can also use multiple condition for such case.

In below condition we used or condition with awk if condition and like to print any line that has first column match with certain strings

ssirohi@jarvis:~$ awk -F: '{if($1=="root" || ($1=="ssirohi")) {print $0}}' /etc/passwd    
root:x:0:0:root:/root:/usr/bin/zsh                                                        
ssirohi:x:1000:1000:ssirohi,,,:/home/ssirohi:/bin/bash                                    

But if we like to use and operator with condition we can use that too

ssirohi@jarvis:/etc$ ls -l | awk '{if (NF>9 && $9=="os-release") {print $9,$10,$11}}'   
os-release -> ../usr/lib/os-release                                                     

Awk if else statements

In awk,we can also use if else statements which has could work like same as any nested if else statements work

Synatx

if (conditional)
	action1
else
	action2

or 

awk '{if(condition) {action} else {action}}'

In this scenario if any reasons if condition did successful, we would have another action to take place. Which would be like final action for this statement.

$ df -hTP | awk '{if($2~"ext") {print $1 "\t" "xfs" "\t" $3 " Need-Updated"} else{print $1 "\t" $2 "\t" $3}}'| column -t  
Filesystem  Type      Size                                                                                                              
udev        devtmpfs  7.8G                                                                                                              
tmpfs       tmpfs     1.6G                                                                                                              
/dev/sda3   xfs       452G  Need-Upgrade                                                                                                
tmpfs       tmpfs     7.8G                                                                                                              
tmpfs       tmpfs     5.0M                                                                                                              
tmpfs       tmpfs     7.8G                                                                                                              
/dev/sdb1   xfs       1.8T  Need-Upgrade                                                                                                
/dev/sdc1   xfs       916G  Need-Upgrade                                                                                                
/dev/sda1   vfat      796M                                                                                                              
tmpfs       tmpfs     1.6G                                                                                                              

In above example, we have created data-base for inventory purpose that any ext file-system need to upgrade to xfs file-system. Here we have manage to change fields that contain ext with xfs.

In similar way we can also append another or multiple if conditions in awk statements, So let’s try to start multiple if else statements.

if else if statements

Like earlier we work with if and if else conditions statements we can also work to manage multiple if else conditions in awk to work some more conditions within awk.

Syntax

if (condition)
	action
else if (condition)
	action
else
	action

or 

awk '{if(condition) {action} else if(condition) {action} else {action}}'

Let’s just try to make another examples like above, play with df output. This time we need to remove file-system those has vfat.

$ df -hTP | awk '{if($2~"ext") {print $1 "\t" "xfs" "\t" $3} else if($2=="vfat"){print $1 " -- -- Remove-it"} else{print $1 "\t" $2 "\t" $3}}'| column -t
Filesystem  Type      Size                                                                                                                                             
udev        devtmpfs  7.8G                                                                                                                                             
tmpfs       tmpfs     1.6G                                                                                                                                             
/dev/sda3   xfs       452G                                                                                                                                             
tmpfs       tmpfs     7.8G                                                                                                                                             
tmpfs       tmpfs     5.0M                                                                                                                                             
tmpfs       tmpfs     7.8G                                                                                                                                             
/dev/sdb1   xfs       1.8T                                                                                                                                             
/dev/sdc1   xfs       916G                                                                                                                                             
/dev/sda1   --        --    Remove-it                                                                                                                                  
tmpfs       tmpfs     1.6G                                                                                                                                             

So above way we can work with awk in multiple conditions. Awk is really powerful tool with which we can we can manipulate many easy text format things. Above we just try to provide some common example in which we use awk if, if else and if else if else conditions.