Awk If Statement

Awk If Statement

In this awk tutorial, let us review awk conditional if statements with practical examples.

Normally conditional statement checks the condition, before performing any action. If the condition is true action(s) are performed. Similarly action can be performed if the condition is false.

Conditional statement starts with the keyword called "if". Awk supports three different kind of if statement.

awk If Statement

Single Action: Simple If statement is used to check the conditions, if the condition returns true, it performs its corresponding action(s).

Syntax:
if (conditional-expression)
	action
  • if is a keyword
  • conditional-expression – expression to check conditions
  • action – any awk statement to perform action.

Syntax:
{
    if (conditional-expression) {
	action1;
	action2;
    }
}

If the condition is true, all the actions enclosed in braces will be performed in the given order. After all the actions are performed it continues to execute the next statements.

example:

~] cat students-marks.txt 
Jones   2143 78 84 77
Gondrol 2321 56 58 45
RinRao  2122 38 37
Edwin   2537 78 67 45
Dayan   2415 30 47

~] cat example1.awk 
#!/usr/bin/awk -f

{
    if ($3 =="" || $4 == "" || $5 == "") {
        print "Some score for the student",$1,"is missing";
    }
}

~] ./example1.awk students-marks.txt 
Some score for the student RinRao is missing
Some score for the student Dayan is missing

awk if-else Statement

In the above simple awk If statement, there is no set of actions in case if the condition is false. In the awk If Else statement you can give the list of action to perform if the condition is false. If the condition returns true action1 will be performed, if the condition is false action 2 will be performed.

Syntax:
{
    if (conditional-expression) {
        action1
    }
    else {
        action2
    }
}

example:

~] cat students-marks.txt 
Jones   2143 78 84 77
Gondrol 2321 56 58 45
RinRao  2122 38 37
Edwin   2537 78 67 45
Dayan   2415 30 47

~] cat example2.awk 
#!/usr/bin/awk -f

{
    if ($3 >=35 && $4 >= 35 && $5 >= 35) {
        print $0,"=>","Pass";
    }
    else {
        print $0,"=>","Fail";
    }
}

~] ./example2.awk students-marks.txt 
Jones   2143 78 84 77 => Pass
Gondrol 2321 56 58 45 => Pass
RinRao  2122 38 37 => Fail
Edwin   2537 78 67 45 => Pass
Dayan   2415 30 47 => Fail

The condition for Pass is all the test score mark should be greater than or equal to 35. So all the test scores are checked if greater than 35, then it prints the whole line and string "Pass", else i.e even if any one of the test score doesn’t meet the condition, it prints the whole line and prints the string "Fail".

awk if-else-if statement

{
    if(conditional-expression1) {
        action1;
    }
    else if(conditional-expression2) {
	action2;
    }
    else if(conditional-expression3) {
	action3;
	.
	.
    }
    else {
	action n;
    }
}
  • If the conditional-expression1 is true then action1 will be performed.
  • If the conditional-expression1 is false then conditional-expression2 will be checked, if its true, action2 will be performed and goes on like this. Last else part will be performed if none of the conditional-expression is true.

example:

~] cat students-marks.txt 
Jones   2143 78 84 77
Gondrol 2321 56 58 45
RinRao  2122 38 37
Edwin   2537 78 67 45
Dayan   2415 30 47

~] cat example3.awk 
#!/usr/bin/awk -f

{
    total = $3 + $4 + $5;
    avg = total / 3;
    if ( avg >= 90 ) {
        grade = "A";
    }
    else if ( avg >= 80) {
        grade = "B";
    }
    else if (avg >= 70) {
        grade = "C";
    }
    else {
        grade = "D";
    }

    print $0,"=>",grade;
}

~] ./example3.awk students-marks.txt 
Jones   2143 78 84 77 => C
Gondrol 2321 56 58 45 => D
RinRao  2122 38 37 => D
Edwin   2537 78 67 45 => D
Dayan   2415 30 47 => D

SUBSCRIBE FOR NEW ARTICLES

@
comments powered by Disqus