Awk series: How to Use flow control statements in awk

Source: Internet
Author: User

Awk series: How to Use flow control statements in awk
GuideWhen you review all the awk instances we have covered so far, from the beginning of the awk series, you will notice that all the commands of various instances are executed sequentially, that is, one by one. However, in some cases, we may want to perform text filtering based on some conditions, that is, the statements allowed by flow control statements.


There are a variety of flow control statements in awk programming, including:

  • If-else statement
  • For statement
  • While statement
  • Do-while statement
  • Break statement
  • Continue statement
  • Next statement
  • Nextfile statement
  • Exit statement

However, for this part of this series, we will elaborate on: if-else, for, while, And do while statements. Remember, we have introduced how to use the next statement of awk in part 1 of this awk series.

1. if-else statement

As you think. The if statement syntax is similar to the if statement in shell:

If (condition 1) {Action 1} else {Action 2}

In the preceding syntax, condition 1 and condition 2 are awk expressions, and Action 1 and Action 2 are the awk commands executed when their respective conditions are met.

If condition 1 is true, Action 1 is executed and exited the if statement. Otherwise, action 2 is executed.

The if statement can also be extended to the following if-else_if-else statement:

If (condition 1) {Action 1} elseif (condition 2) {Action 2} else {Action 3}

In the preceding format, if condition 1 is true, Action 1 is executed and exited the if statement. Otherwise, condition 2 is evaluated and if the value is true, then Action 2 is executed and exited the if statement. However, when condition 2 is false, Action 3 is executed and exits the if statement.

This is an example of using the if statement. We have a list of users and their ages stored in the file users.txt.

We need to print a list to show whether the user name and the user's age are less than or more than 25 years old.

aaronkilik@tecMint~$catusers.txtSarahL35FAaronKili40MJohnDoo20MKiliSeth49M

We can write a short shell script to execute our work in the above text, which is the content of the script:

#!/bin/bashawk'{      if($3<=25){ print"User",$1,$2,"islessthan25yearsold.";     }else{     print"User",$1,$2,"ismorethan25yearsold"; }}'~/users.txt

Save the file and exit. Run the script as follows:

$chmod+xtest.sh$./test.sh

Output example

UserSarahLismorethan25yearsoldUserAaronKiliismorethan25yearsoldUserJohnDooislessthan25yearsold.UserKiliSethismorethan25yearsold
2. for statement

If you want to execute some awk commands in a loop, the for statement provides you with an appropriate method for doing this, in the following format:

For (counter initialization; test conditions; counter addition) {action}

Here, this method is defined by using a counter to control loop execution. First you need to initialize this counter and then run it for test conditions. If it is true, execute these actions and add the counter. When the counter does not meet the conditions, the cycle ends.

When we want to print numbers 0 to 10, the following awk command shows how the for statement works:

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

Output example

0 12345678910
3. while statement

The traditional syntax of the while statement is as follows:

While (condition) {action}

This condition is an awk expression, and the action is the awk Command executed when the condition is true.

The following is a script that uses the while statement to print numbers 0 to 10:

#!/bin/bash awk'BEGIN{counter=0; while(counter<=10){ printcounter; counter+=1; }}'

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

$chmod+xtest.sh$./test.sh

Output example

0 12345678910
4. do while statement

It is a variant of the while statement in the above text and has the following syntax:

Do {action} while (condition)

The slight difference is that in the do while statement, the awk command is executed before the evaluate condition. In the example of the while statement above, we can modify the awk command in the test. sh script as follows to describe the usage of the do while statement:

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

After modifying the script, save the file and exit. Run the script as follows:

$chmod+xtest.sh$./test.sh

Output example

0 12345678910
5. Summary

This is not a comprehensive guide to awk process control statements. As I mentioned earlier, there are several other process control statements in awk.

Even so, this part of the awk series allows you to understand the basic concept of how an awk command is executed based on certain conditions.

You can also learn more about the other process control statements to obtain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.