当前位置:网站首页>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
边栏推荐
- Group programming ladder race - exercise set l2-002 linked list de duplication
- Redis sentinel mechanism
- Codeforces Global Round 21(A-E)
- awk从入门到入土(18)gawk线上手册
- Technology sharing | MySQL parallel DDL
- yolov5 xml数据集转换为VOC数据集
- How to solve the problem of computer jam and slow down
- awk从入门到入土(9)循环语句
- 【无标题】转发最小二乘法
- 力扣今日题-1200. 最小绝对差
猜你喜欢
awk从入门到入土(12)awk也可以写脚本,替代shell
How college students choose suitable computers
Sports [running 01] a programmer's half horse challenge: preparation before running + adjustment during running + recovery after running (experience sharing)
【无标题】转发最小二乘法
Openfeign service interface call
到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
es6总结
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
ArcGIS application (XXII) ArcMap loading lidar Las format data
随机推荐
根据数字显示中文汉字
Leetcode topic [array] -136- numbers that appear only once
awk从入门到入土(11)awk getline函数详解
DM8 command line installation and database creation
Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid
awk从入门到入土(6)正则匹配
AcWing 244. Enigmatic cow (tree array + binary search)
MySQL relearn 1-centos install mysql5.7
没有Kubernetes怎么玩Dapr?
到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
Need help resetting PHP counters - PHP
Educational Codeforces Round 115 (Rated for Div. 2)
Cannot click button when method is running - C #
Manjaro install wechat
Question 49: how to quickly determine the impact of IO latency on MySQL performance
@Role of requestparam annotation
09 softmax regression + loss function
awk从入门到入土(18)gawk线上手册
Basic discipline formula and unit conversion
Webapi interview question summary 01