当前位置:网站首页>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
边栏推荐
- 2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
- Technology sharing | MySQL parallel DDL
- awk从入门到入土(9)循环语句
- ArcGIS application (XXII) ArcMap loading lidar Las format data
- System disk expansion in virtual machine
- Need help resetting PHP counters - PHP
- [BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
- SSRF vulnerability exploitation - attack redis
- DM8 database recovery based on point in time
- How to solve the problem of computer jam and slow down
猜你喜欢

Display Chinese characters according to numbers

How can we make a monthly income of more than 10000? We media people with low income come and have a look

ctfshow web255 web 256 web257

What should I do if there is a problem with the graphics card screen on the computer

Live in a dream, only do things you don't say

Go zero micro service practical series (IX. ultimate optimization of seckill performance)

Codeforces Global Round 21(A-E)

Manjaro install wechat

Newh3c - routing protocol (RIP, OSPF)

微服务入门:Gateway网关
随机推荐
Technology sharing | MySQL parallel DDL
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
string. Format without decimal places will generate unexpected rounding - C #
SQL statement view SQL Server 2005 version number
1. Kalman filter - the best linear filter
Sequence model
@Role of pathvariable annotation
FOC control
High order phase difference such as smear caused by myopic surgery
DM database password policy and login restriction settings
ctfshow web255 web 256 web257
Use Alibaba cloud NPM image acceleration
随机事件的关系与运算
C#实现一个万物皆可排序的队列
Openfeign service interface call
Codeforces Round #793 (Div. 2)(A-D)
awk从入门到入土(4)用户自定义变量
Const string inside function - C #
Turn: excellent managers focus not on mistakes, but on advantages
09 softmax regression + loss function