当前位置:网站首页>awk从入门到入土(9)循环语句
awk从入门到入土(9)循环语句
2022-07-04 08:38:00 【奇妙之二进制】
除了前面介绍的条件语句,AWK 还提供了循环语句。该语句的作用就是当条件为真时重复执行一系列的命令。本章将讲解 AWK 中循环语句的使用方法。
For
For 循环的语法如下:
for (initialisation; condition; increment/decrement)
action
for 语句首先执行初始化动作( initialisation ),然后再检查条件( condition )。如果条件为真,则执行动作( action ),然后执行递增( increment )或者递减( decrement )操作。只要条件为真循环就会一直执行。每次循环结束都会进条件检查,若条件为假则结束 循环。下面的例子使用 For 循环输出数字 1 至 5:
$ awk 'BEGIN { for (i = 1; i <= 5; ++i) print i }'
执行上面的命令可以得到如下结果:
1
2
3
4
5
While
While 循环会一直执行动作直到逻辑条件为假为止。其使用方法如下:
while (condition)
action
AWK 首先检查条件是否为真,若条件为真则执行动作。此过程一直重复直到条件为假时,则停止。下面是使用 While 循环输出数字 1 到 5 的例子:
$ awk 'BEGIN {i = 1; while (i < 6) { print i; ++i } }'
执行上面的命令可以得到如下的结果:
1
2
3
4
5
Do-While
Do-While 循环与 While 循环相似,但是 Do-While 的条件测试放到了循环的尾部。下面是 do-while 的语法:
do
action
while (condition)
在 do-while 循环中,无论条件是真是假,循环语句至少执行一次,执行后检查条件真假。下面是使用 do-While 循环输出数字 1 到 5 的例子:
$ awk 'BEGIN {i = 1; do { print i; ++i } while (i < 6) }'
执行上面的命令可以得到如下的结果:
1
2
3
4
5
Break
顾名思义,break 用以结束循环过程。在下面的示例子中,当计算的和大于 50 的时候使用 break 结束循环过程:
$ awk 'BEGIN {
sum = 0;
for (i = 0; i < 20; ++i) {
sum += i;
if (sum > 50) break;
else
print "Sum =", sum
}
}'
执行上面的命令可以得到如下的结果:
Sum = 0
Sum = 1
Sum = 3
Sum = 6
Sum = 10
Sum = 15
Sum = 21
Sum = 28
Sum = 36
Sum = 45
Continue
Continue 语句用于在循环体内部结束本次循环,从而直接进入下一次循环迭代。当我们希望跳过循环中某处数据处理时就会用到 Continue。下面的例子输出 1 到 20 之间的偶数:
$ awk 'BEGIN {for (i = 1; i <= 20; ++i) {if (i % 2 == 0) print i ; else continue} }'
执行上面的命令可以得到如下的结果:
2
4
6
8
10
12
14
16
18
20
Exit
Exit 用于结束脚本程序的执行。该函数接受一个整数作为参数表示 AWK 进程结束状态。 如果没有提供该参数,其默认状态为 0 。下面例子中当和大于 50 时结束 AWK 程序。
$ awk 'BEGIN {
sum = 0;
for (i = 0; i < 20; ++i) {
sum += i;
if (sum > 50)
exit(10);
else print "Sum =", sum;
}
}'
执行上面的命令可以得到如下的结果:
Sum = 0
Sum = 1
Sum = 3
Sum = 6
Sum = 10
Sum = 15
Sum = 21
Sum = 28
Sum = 36
Sum = 45
让我们检查一下脚本执行后的返回状态:
$ echo $?
执行上面的命令可以得到如下的结果:
19
边栏推荐
- How to play dapr without kubernetes?
- How to use C language code to realize the addition and subtraction of complex numbers and output structure
- PHP converts seconds to timestamps - PHP
- Do you know about autorl in intensive learning? A summary of articles written by more than ten scholars including Oxford University and Google
- FRP intranet penetration, reverse proxy
- Convert datetime string to datetime - C in the original time zone
- How can we make a monthly income of more than 10000? We media people with low income come and have a look
- 小程序容器技术与物联网 IoT 可以碰撞出什么样的火花
- Li Kou today's question -1200 Minimum absolute difference
- How to play dapr without kubernetes?
猜你喜欢
没有Kubernetes怎么玩Dapr?
The basic syntax of mermaid in typera
转:优秀的管理者,关注的不是错误,而是优势
snipaste 方便的截图软件,可以复制在屏幕上
DM8 database recovery based on point in time
运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
Démarrage des microservices: passerelle
Manjaro install wechat
Basic operations of databases and tables ----- view data tables
随机推荐
Figure guessing game
Xcode 6 swift code completion does not work properly - Xcode 6 swift code completion not working
埃氏筛+欧拉筛+区间筛
FOC控制
How to play dapr without kubernetes?
Developers really review CSDN question and answer function, and there are many improvements~
运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)
AcWing 244. Enigmatic cow (tree array + binary search)
学习Nuxt.js
Scanf read in data type symbol table
go-zero微服务实战系列(九、极致优化秒杀性能)
团体程序设计天梯赛-练习集 L2-002 链表去重
R language ggplot2 visualization: ggplot2 visualization grouping box diagram, place the legend and title of the visualization image on the top left of the image and align them to the left, in which th
Collections in Scala
ArcGIS application (XXII) ArcMap loading lidar Las format data
C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code
Difference between static method and non static method (advantages / disadvantages)
Sports [running 01] a programmer's half horse challenge: preparation before running + adjustment during running + recovery after running (experience sharing)
DM database password policy and login restriction settings
Take you to master the formatter of visual studio code