当前位置:网站首页>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
边栏推荐
- 没有Kubernetes怎么玩Dapr?
- ArcGIS application (XXII) ArcMap loading lidar Las format data
- Group programming ladder race - exercise set l2-002 linked list de duplication
- 1. Kalman filter - the best linear filter
- System disk expansion in virtual machine
- Codeforces Round #793 (Div. 2)(A-D)
- Technology sharing | MySQL parallel DDL
- Xcode 6 swift code completion does not work properly - Xcode 6 swift code completion not working
- Conversion of yolov5 XML dataset to VOC dataset
- Developers really review CSDN question and answer function, and there are many improvements~
猜你喜欢
小程序容器技术与物联网 IoT 可以碰撞出什么样的火花
Sports [running 01] a programmer's half horse challenge: preparation before running + adjustment during running + recovery after running (experience sharing)
What if the wireless network connection of the laptop is unavailable
Basic operations of databases and tables ----- view data tables
ctfshow web255 web 256 web257
High order phase difference such as smear caused by myopic surgery
4 small ways to make your Tiktok video clearer
Collections in Scala
yolov5 xml数据集转换为VOC数据集
MySQL relearn 1-centos install mysql5.7
随机推荐
What if I forget the router password
Redis sentinel mechanism
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
PHP converts seconds to timestamps - PHP
Group programming ladder race - exercise set l2-002 linked list de duplication
Example analysis of C # read / write lock
go-zero微服务实战系列(九、极致优化秒杀性能)
Using the rate package for data mining
Use preg_ Match extracts the string into the array between: & | people PHP
一文了解數據异常值檢測方法
DM database password policy and login restriction settings
deno debugger
力扣今日题-1200. 最小绝对差
DM8 command line installation and database creation
DM8 database recovery based on point in time
System disk expansion in virtual machine
SSRF vulnerability exploitation - attack redis
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Fault analysis | MySQL: unique key constraint failure
How to re enable local connection when the network of laptop is disabled