当前位置:网站首页>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
边栏推荐
- C#实现一个万物皆可排序的队列
- Moher College webmin unauthenticated remote code execution
- How to improve your system architecture?
- Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
- Newh3c - network address translation (NAT)
- ArcGIS application (XXII) ArcMap loading lidar Las format data
- FRP intranet penetration, reverse proxy
- Group programming ladder race - exercise set l2-002 linked list de duplication
- 一文了解數據异常值檢測方法
- Comprendre la méthode de détection des valeurs aberrantes des données
猜你喜欢

How to play dapr without kubernetes?

ES6 summary

DM8 database recovery based on point in time

manjaro安装微信

DM8 command line installation and database creation

How to improve your system architecture?

Do you know about autorl in intensive learning? A summary of articles written by more than ten scholars including Oxford University and Google

Question 49: how to quickly determine the impact of IO latency on MySQL performance

Newh3c - network address translation (NAT)

Collections in Scala
随机推荐
Basic operations of databases and tables ----- view data tables
Getting started with microservices: gateway gateway
Using the rate package for data mining
Unity write word
2022 gas examination registration and free gas examination questions
deno debugger
没有Kubernetes怎么玩Dapr?
埃氏筛+欧拉筛+区间筛
小程序容器技术与物联网 IoT 可以碰撞出什么样的火花
WordPress get_ Users() returns all users with comparison queries - PHP
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
微服務入門:Gateway網關
MySQL relearn 1-centos install mysql5.7
Do you know about autorl in intensive learning? A summary of articles written by more than ten scholars including Oxford University and Google
What if I forget the router password
[test de performance] lire jmeter
System disk expansion in virtual machine
微服务入门:Gateway网关
Moher college phpMyAdmin background file contains analysis traceability
Moher College webmin unauthenticated remote code execution