Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use flow Control statement in awk

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use process control statements in awk. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

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 cover: if-else, for, while, and do while statements. Keep in mind that we have described how to use awk's next statement in part 6 of this awk series.

1. If-else statement

That's what you think. The syntax of the if statement is similar to the if statement in shell:

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

In the above syntax, conditions 1 and 2 are awk expressions, while actions 1 and 2 are awk commands that are executed when their respective conditions are met.

When condition 1 is satisfied, which means it is true, then Action 1 is executed and exits 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} else if (condition 2) {Action 2} else {Action 3}

For the above form, if condition 1 is true, then action 1 is executed and exits the if statement, otherwise condition 2 is evaluated and if the value is true, then action 2 is executed and exits 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, where we have a list of users and their ages, stored in the file users.txt.

We want to print a list showing whether the user's name and age are less than or over 25 years old.

Aaronkilik@tecMint ~ $cat users.txt Sarah L 35 F Aaron Kili 40 M John Doo 20 M Kili Seth 49 M

We can write a short shell script to perform our work above, which is what the script says:

#! / bin/bash awk'{if ($3

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report