How to Use Flow Control Statements in Awk – Part 12

[‘

n

When you review all the Awk examples we have covered so far, right from the start of the Awk series, you will notice that all the commands in the various examples are executed sequentially, that is one after the other. But in certain situations, we may want to run some text filtering operations based on some conditions, that is where the approach of flow control statements sets in.

n

Use Flow Control Statements in Awk
Use Flow Control Statements in Awk

n

There are various flow control statements in Awk programming and these include:

n

    n

  1. if-else statement
  2. n

  3. for statement
  4. n

  5. while statement
  6. n

  7. do-while statement
  8. n

  9. break statement
  10. n

  11. continue statement
  12. n

  13. next statement
  14. n

  15. nextfile statement
  16. n

  17. exit statement
  18. n

n

However, for the scope of this series, we shall expound on: if-else, for, while and do while statements. Remember that we already walked through how to use next statement in Part 6 of this Awk series.

n

1. The if-else Statement

n

The expected syntax of the if statement is similar to that of the shell if statement:

n

if  (condition1) {rn     actions1rn}rnelse {rn      actions2rn}rn

n

In the above syntax, condition1 and condition2 are Awk expressions, and actions1 and actions2 are Awk commands executed when the respective conditions are satisfied.

n

When condition1 is satisfied, meaning it’s true, then actions1 is executed and the if statement exits, otherwise actions2 is executed.

n

The if statement can also be expanded to a if-else_if-else statement as below:

n

if (condition1){rn     actions1rn}rnelse if (conditions2){rn      actions2rn}rnelse{rn     actions3rn}rn

n

For the form above, if condition1 is true, then actions1 is executed and the if statement exits, otherwise condition2 is evaluated and if it is true, then actions2 is executed and the if statement exits. However, when condition2 is false then, actions3 is executed and the if statement exits.

n

Here is a case in point of using if statements, we have a list of users and their ages stored in the file, users.txt.

n

We want to print a statement indicating a user’s name and whether the user’s age is less or more than 25 years old.

n

[emailxa0protected] ~ $ cat users.txtrnSarah Lttt35    tFrnAaron Kilitt40    tMrnJohn  Doott20    tMrnKili  Sethtt49    tM    rn

n

We can write a short shell script to carry out our job above, here is the content of the script:

n

#!/bin/bashrnawk ' { rn        if ( $3 <= 25 ){rn           print "User",$1,$2,"is less than 25 years old." ;rn        }rn        else {rn           print "User",$1,$2,"is more than 25 years old" ; rn}rn}'    ~/users.txtrn

n

Then save the file and exit, make the script executable and run it as follows:

n

$ chmod +x test.shrn$ ./test.shrn

n

Sample Output

n

User Sarah L is more than 25 years oldrnUser Aaron Kili is more than 25 years oldrnUser John Doo is less than 25 years old.rnUser Kili Seth is more than 25 years oldrn

n

2. The for Statement

n

In case you want to execute some Awk commands in a loop, then the for statement offers you a suitable way to do that, with the syntax below:

n

Here, the approach is simply defined by the use of a counter to control the loop execution, first you need to initialize the counter, then run it against a test condition, if it is true, execute the actions and finally increment the counter. The loop terminates when the counter does not satisfy the condition.

n

for ( counter-initialization; test-condition; counter-increment ){rn      actionsrn}rn

n

The following Awk command shows how the for statement works, where we want to print the numbers 0-10:

n

$ awk 'BEGIN{ for(counter=0;counter<=10;counter++){ print counter} }'rn

n

Sample Output

n

0rn1rn2rn3rn4rn5rn6rn7rn8rn9rn10rn

n

3. The while Statement

n

The conventional syntax of the while statement is as follows:

n

while ( condition ) {rn          actionsrn}rn

n

The condition is an Awk expression and actions are lines of Awk commands executed when the condition is true.

n

Below is a script to illustrate the use of while statement to print the numbers 0-10:

n

#!/bin/bashrnawk ' BEGIN{ counter=0 ;rn         rn        while(counter<=10){rn              print counter;rn              counter+=1 ;rn             rn}rn}  rn

n

Save the file and make the script executable, then run it:

n

$ chmod +x test.shrn$ ./test.shrn

n

Sample Output

n

0rn1rn2rn3rn4rn5rn6rn7rn8rn9rn10rn

n

4. The do while Statement

n

It is a modification of the while statement above, with the following underlying syntax:

n

do {rn     actionsrn}rn while (condition) rn

n

The slight difference is that, under do while, the Awk commands are executed before the condition is evaluated. Using the very example under while statement above, we can illustrate the use of do while by altering the Awk command in the test.sh script as follows:

n

#!/bin/bashrnrnawk ' BEGIN{ counter=0 ;  rn        do{rn            print counter;  rn            counter+=1 ;    rn        }rn        while (counter<=10)   rn} rn'rn

n

After modifying the script, save the file and exit. Then make the script executable and execute it as follows:

n

$ chmod +x test.shrn$ ./test.shrn

n

Sample Output

n

0rn1rn2rn3rn4rn5rn6rn7rn8rn9rn10rn

n

Conclusion

n

This is not a comprehensive guide regarding Awk flow control statements, as I had mentioned earlier on, there are several other flow control statements in Awk.

n

Nonetheless, this part of the Awk series should give you a clear fundamental idea of how execution of Awk commands can be controlled based on certain conditions.

n

You can as well expound more on the rest of the flow control statements to gain more understanding on the subject matter. Finally, in the next section of the Awk series, we shall move into writing Awk scripts.

n

‘]