当前位置:网站首页>awk从入门到入土(7)条件语句
awk从入门到入土(7)条件语句
2022-07-04 08:38:00 【奇妙之二进制】
与其实的编程语言一样,AWK 同样提供了条件语句控制程序的执行流程。这一章中我们会介绍 AWK 中条件语句的使用方法。
IF 语句
条件语句测试条件然后根据条件选择执行相应的动作。下面是条件语句的语法:
if (condition)
action
也可以使用花括号来执行一组操作:
if (condition)
{
action-1
action-1
.
.
action-n
}
下面的例子判断数字是奇数还是偶数:
$ awk 'BEGIN {num = 10; if (num % 2 == 0) printf "%d is even number.\n", num }'
执行上面的命令可以得到如下的结果:
10 is even number.
IF - ELSE 语句
if-else语句中允许在条件为假时执行另外一组的动作。下面为 if-else 的语法格式:
if (condition)
action-1
else
action-2
其中,条件为真时执行 action-1,条件为假时执行 action-2。下面是使用该语句判断数字是否为偶数的例子:
$ awk 'BEGIN {num = 11;
if (num % 2 == 0) printf "%d is even number.\n", num;
else printf "%d is odd number.\n", num
}'
执行上面的操作可以得到如下的结果:
11 is odd number.
if-else-if
我们可以很轻松地使用多个 if-else 语句构造 if-else-if 梯从而实现多个条件的判断。示例如下:
$ awk 'BEGIN {
a=30;
if (a==10)
print "a = 10";
else if (a == 20)
print "a = 20";
else if (a == 30)
print "a = 30";
}'
执行上面的命令可以得到如下的结果:
a = 30
边栏推荐
- Parallel shift does not provide any acceleration - C #
- 如何通过antd的upload控件,将图片以文件流的形式发送给服务器
- Unity write word
- How to send pictures to the server in the form of file stream through the upload control of antd
- What does range mean in PHP
- 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
- PHP session variable passed from form - PHP
- OpenFeign 服务接口调用
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- Add log file to slim frame - PHP
猜你喜欢
Developers really review CSDN question and answer function, and there are many improvements~
Leetcode topic [array] -136- numbers that appear only once
From scratch, use Jenkins to build and publish pipeline pipeline project
es6总结
OpenFeign 服务接口调用
How to choose solid state hard disk and mechanical hard disk in computer
Wechat has new functions, and the test is started again
What should I do if there is a problem with the graphics card screen on the computer
A method for detecting outliers of data
ES6 summary
随机推荐
Flutter 集成 amap_flutter_location
团体程序设计天梯赛-练习集 L1-006 连续因子
Fault analysis | MySQL: unique key constraint failure
Cannot click button when method is running - C #
How to solve the problem that computers often flash
微服务入门:Gateway网关
Educational Codeforces Round 119 (Rated for Div. 2)
转:优秀的管理者,关注的不是错误,而是优势
SSRF vulnerability exploitation - attack redis
What if I forget the router password
DM8 uses different databases to archive and recover after multiple failures
没有Kubernetes怎么玩Dapr?
1、卡尔曼滤波-最佳的线性滤波器
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
Unity-写入Word
Moher College phpmailer remote command execution vulnerability tracing
How college students choose suitable computers
Developers really review CSDN question and answer function, and there are many improvements~
Basic operations of databases and tables ----- view data tables
DM8 tablespace backup and recovery