How to use awk if else statement

Awk is a programming language allows for quick manipulation of structured data types and produces formatted output. The name is made by joining first letters of the authors name, Aho, Weinberger, and Kernighan.

The awk command is commonly used to search and manipulate text. During this process, it will search for lines in one or more of the files that contain the given pattern, and then execute the appropriate action.

Awk supports conditional statements to control the flow of the program. The syntax is inspired from C programming language.

This article is going to show you how to use various if-else-elseif statements syntax in AWK.

if statement

The if statement checks the conditions and if they are true, it executes its corresponding action(s).

if (condition){
    statement
}

AWK if example

search in employee.txt contents and print out the department name, age if user whose id is 11002

## employee.txt
11001  Sales     45   $3000
11002  HR        32   $1500
11003  Marketing 26   $1200
11004  HR        25   $2500
awk '{
        if($1 =="11002")
        {
            print "Department: ",$2,"\n";
            print "Age: ",$3,"\n";
        }
}' employee.txt

if else statement

With the expression if-else, you can specify the list of actions that should be taken if the condition is false. If the condition returns true, statement1 will be executed, and if it returns false, statement2 will be executed.

if (condition){
    statement1
}
else{
    statement2
}

AWK if-else example

Let's say you want to print the salary information of the employees under the age of 40 and print the age information for the others. This task can be accomplished using the awk script below.

There is only one employee over the age of 40 in the employee.txt file, all the others are under the age of 40.

awk '{
if($3 <= 40)
{
    print "The salary of ",$1, " is ", $4, "\n"
}
else
{
    print "The age of ",$1, " is ", $3, "\n"
}
}' employee.txt

if elseif statement

Multiple if conditions are used in this conditional statement to execute a statement. Upon failure of the first condition, it checks the second one. A second false condition leads to a third, then the next. All conditions must return false in order for the else part to be executed.

if (condition){
    statement1
}
else if(condition){
    statement2
}
else if(condition){
    statement3
}
……
else{
    statementN
}

AWK if-elseif example

Let's say you want to print the salary information of the employees under the age of 30, then the under-40, then the over-40.

awk '{
if($3 <= 30)
{
    print "The salary of ",$1, " is ", $4, "\n"
}
elseif($3 <= 40)
{
    print "The salary of ",$1, " is ", $4, "\n"
}
else{
    print "The salary of ",$1, " is ", $4, "\n"
}
}' employee.txt

AWK : Ternary operator

You can use ternary operators instead of if-else statements. A true condition would cause statement1 to execute, a false condition would cause statement2 to execute.

(condition) ? statement-1: statement-2
Click to rate this post!
[Total: 18 Average: 5]

Leave a Comment