当前位置:网站首页>Awk from getting started to digging in (9) circular statement
Awk from getting started to digging in (9) circular statement
2022-07-04 08:46:00 【Wonderful binary】
In addition to the conditional statements introduced above ,AWK Circular statements are also provided . The function of this statement is to repeat a series of commands when the condition is true . This chapter will cover AWK How to use circular statements in .
For
For The syntax of the loop is as follows :
for (initialisation; condition; increment/decrement)
action
for Statement first performs the initialization action ( initialisation ), Then check the conditions ( condition ). If the condition is true , Then perform the action ( action ), Then perform incremental ( increment ) Or decreasing ( decrement ) operation . As long as the condition is true, the loop will always execute . Condition check will be carried out at the end of each cycle , If the condition is false, it ends loop . The following example uses For Loop out numbers 1 to 5:
$ awk 'BEGIN { for (i = 1; i <= 5; ++i) print i }'
You can get the following result by executing the above command :
1
2
3
4
5
While
While The loop will execute the action until the logical condition is false . Its usage is as follows :
while (condition)
action
AWK First, check whether the condition is true , If the condition is true, execute the action . This process is repeated until the condition is false , Then stop . Here's how to use While Loop out numbers 1 To 5 Example :
$ awk 'BEGIN {i = 1; while (i < 6) { print i; ++i } }'
Execute the above command to get the following result :
1
2
3
4
5
Do-While
Do-While Circulation and While Cyclic similarity , however Do-While The conditional test of is placed at the end of the loop . Here is do-while The grammar of :
do
action
while (condition)
stay do-while In circulation , Whether the condition is true or false , The loop statement is executed at least once , Check whether the conditions are true or false after execution . Here's how to use do-While Loop out numbers 1 To 5 Example :
$ awk 'BEGIN {i = 1; do { print i; ++i } while (i < 6) }'
Execute the above command to get the following result :
1
2
3
4
5
Break
seeing the name of a thing one thinks of its function ,break To end the cycle . In the example shown below , When the calculated sum is greater than 50 When you use break End the cycle process :
$ awk 'BEGIN {
sum = 0;
for (i = 0; i < 20; ++i) {
sum += i;
if (sum > 50) break;
else
print "Sum =", sum
}
}'
Execute the above command to get the following result :
Sum = 0
Sum = 1
Sum = 3
Sum = 6
Sum = 10
Sum = 15
Sum = 21
Sum = 28
Sum = 36
Sum = 45
Continue
Continue Statement is used to end the loop inside the loop body , So as to directly enter the next cycle iteration . It is used when we want to skip data processing somewhere in the loop Continue. The following example outputs 1 To 20 Even number between :
$ awk 'BEGIN {for (i = 1; i <= 20; ++i) {if (i % 2 == 0) print i ; else continue} }'
Execute the above command to get the following result :
2
4
6
8
10
12
14
16
18
20
Exit
Exit Used to end the execution of a script program . This function takes an integer as an argument AWK Process end state . If the parameter is not provided , Its default state is 0 . In the following example, when and are greater than 50 End of time AWK Program .
$ awk 'BEGIN {
sum = 0;
for (i = 0; i < 20; ++i) {
sum += i;
if (sum > 50)
exit(10);
else print "Sum =", sum;
}
}'
Execute the above command to get the following result :
Sum = 0
Sum = 1
Sum = 3
Sum = 6
Sum = 10
Sum = 15
Sum = 21
Sum = 28
Sum = 36
Sum = 45
Let's check the return status of the script after execution :
$ echo $?
Execute the above command to get the following result :
19
边栏推荐
- Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
- How to re enable local connection when the network of laptop is disabled
- What if I forget the router password
- Show server status on Web page (on or off) - PHP
- Display Chinese characters according to numbers
- Industry depression has the advantages of industry depression
- 没有Kubernetes怎么玩Dapr?
- awk从入门到入土(9)循环语句
- Group programming ladder race - exercise set l2-002 linked list de duplication
- PHP converts seconds to timestamps - PHP
猜你喜欢

How to solve the problem that computers often flash

SSRF vulnerability exploitation - attack redis

Manjaro install wechat

Developers really review CSDN question and answer function, and there are many improvements~

一文了解数据异常值检测方法

Ehrlich sieve + Euler sieve + interval sieve

FOC控制

Conversion of yolov5 XML dataset to VOC dataset
![Leetcode topic [array] -136- numbers that appear only once](/img/6d/f2e4b812e5dd872fbeb43732d6f27f.jpg)
Leetcode topic [array] -136- numbers that appear only once

How to re enable local connection when the network of laptop is disabled
随机推荐
Difference between static method and non static method (advantages / disadvantages)
一文了解數據异常值檢測方法
Educational Codeforces Round 115 (Rated for Div. 2)
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
Redis 哨兵机制
Démarrage des microservices: passerelle
How to solve the problem of computer jam and slow down
如何通过antd的upload控件,将图片以文件流的形式发送给服务器
MySQL relearn 1-centos install mysql5.7
PHP session variable passed from form - PHP
C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code
string. Format without decimal places will generate unexpected rounding - C #
Call Baidu map to display the current position
学习Nuxt.js
Collections in Scala
The right way to capture assertion failures in NUnit - C #
随机事件的关系与运算
Manjaro install wechat
Use Alibaba cloud NPM image acceleration
Bishi blog (13) -- oral arithmetic test app