当前位置:网站首页>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
边栏推荐
- Group programming ladder race - exercise set l1-006 continuity factor
- What sparks can applet container technology collide with IOT
- NPM run build error
- [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-
- How to solve the problem of computer jam and slow down
- Flutter 集成 amap_flutter_location
- Example analysis of C # read / write lock
- Learn nuxt js
- Getting started with microservices: gateway gateway
- snipaste 方便的截图软件,可以复制在屏幕上
猜你喜欢
Codeforces Round #803 (Div. 2)(A-D)
DM8 tablespace backup and recovery
Newh3c - network address translation (NAT)
1. Kalman filter - the best linear filter
manjaro安装微信
System disk expansion in virtual machine
Do you know about autorl in intensive learning? A summary of articles written by more than ten scholars including Oxford University and Google
What should I do if there is a problem with the graphics card screen on the computer
Take you to master the formatter of visual studio code
一文了解数据异常值检测方法
随机推荐
ES6 summary
【性能測試】一文讀懂Jmeter
Developers really review CSDN question and answer function, and there are many improvements~
DM8 command line installation and database creation
DM8 database recovery based on point in time
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
@Role of requestparam annotation
Learn nuxt js
根据数字显示中文汉字
团体程序设计天梯赛-练习集 L1-006 连续因子
Internal learning
Comprendre la méthode de détection des valeurs aberrantes des données
[attack and defense world | WP] cat
What should I do if there is a problem with the graphics card screen on the computer
FRP intranet penetration, reverse proxy
Laravel page load problem connection reset - PHP
System disk expansion in virtual machine
Technology sharing | MySQL parallel DDL
ArcGIS application (XXII) ArcMap loading lidar Las format data
Manjaro install wechat