当前位置:网站首页>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
边栏推荐
- Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
- Laravel page load problem connection reset - PHP
- FOC控制
- User login function: simple but difficult
- Xcode 6 swift code completion does not work properly - Xcode 6 swift code completion not working
- What does range mean in PHP
- Redis sentinel mechanism
- Leetcode 23. Merge K ascending linked lists
- Codeforces Round #793 (Div. 2)(A-D)
- The upper layer route cannot Ping the lower layer route
猜你喜欢
4 small ways to make your Tiktok video clearer
go-zero微服务实战系列(九、极致优化秒杀性能)
Basic operations of databases and tables ----- view data tables
【性能測試】一文讀懂Jmeter
ES6 summary
High order phase difference such as smear caused by myopic surgery
DM8 command line installation and database creation
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
09 softmax regression + loss function
OpenFeign 服务接口调用
随机推荐
WordPress get_ Users() returns all users with comparison queries - PHP
Four essential material websites for we media people to help you easily create popular models
Cannot click button when method is running - C #
How to get bytes containing null terminators from a string- c#
2022 examination questions for safety managers of metal and nonmetal mines (underground mines) and examination papers for safety managers of metal and nonmetal mines (underground mines)
[untitled] 2022 polymerization process analysis and polymerization process simulation examination
System disk expansion in virtual machine
deno debugger
DM8 uses different databases to archive and recover after multiple failures
FOC控制
snipaste 方便的截图软件,可以复制在屏幕上
Manjaro install wechat
ctfshow web255 web 256 web257
埃氏筛+欧拉筛+区间筛
User login function: simple but difficult
Collections in Scala
Chrome is set to pure black
广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
Laravel page load problem connection reset - PHP
What sparks can applet container technology collide with IOT