当前位置:网站首页>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
边栏推荐
- Educational Codeforces Round 115 (Rated for Div. 2)
- Sequence model
- ArcGIS application (XXII) ArcMap loading lidar Las format data
- 2022 gas examination registration and free gas examination questions
- Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
- DM8 tablespace backup and recovery
- [CV] Wu Enda machine learning course notes | Chapter 9
- Group programming ladder race - exercise set l1-006 continuity factor
- 1. Kalman filter - the best linear filter
- System disk expansion in virtual machine
猜你喜欢
微服务入门:Gateway网关
1. Getting started with QT
ArcGIS application (XXII) ArcMap loading lidar Las format data
go-zero微服务实战系列(九、极致优化秒杀性能)
manjaro安装微信
How does Xiaobai buy a suitable notebook
[error record] no matching function for call to 'cacheflush' cacheflush();)
Live in a dream, only do things you don't say
Codeforces Round #803 (Div. 2)(A-D)
From scratch, use Jenkins to build and publish pipeline pipeline project
随机推荐
[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-
AcWing 244. Enigmatic cow (tree array + binary search)
Ehrlich sieve + Euler sieve + interval sieve
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Mouse over to change the transparency of web page image
The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
Group programming ladder race - exercise set l1-006 continuity factor
[untitled] 2022 polymerization process analysis and polymerization process simulation examination
Redis 哨兵机制
微服务入门:Gateway网关
广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
What if I forget the router password
awk从入门到入土(18)gawk线上手册
What should I do if there is a problem with the graphics card screen on the computer
根据数字显示中文汉字
awk从入门到入土(4)用户自定义变量
FOC control
Fault analysis | MySQL: unique key constraint failure
Redis sentinel mechanism