当前位置:网站首页>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
边栏推荐
- Difference between static method and non static method (advantages / disadvantages)
- Use preg_ Match extracts the string into the array between: & | people PHP
- Moher college phpMyAdmin background file contains analysis traceability
- Unity-写入Word
- 1. Qt入门
- ctfshow web255 web 256 web257
- Redis sentinel mechanism
- Openfeign service interface call
- DM8 uses different databases to archive and recover after multiple failures
- Go h*ck yourself:online reconnaissance (online reconnaissance)
猜你喜欢

Openfeign service interface call

【性能測試】一文讀懂Jmeter

How does Xiaobai buy a suitable notebook

Comparison between sentinel and hystrix
![[go basics] 1 - go go](/img/e2/d973b9fc9749e1c4755ce7d0ec11a1.png)
[go basics] 1 - go go

Educational Codeforces Round 115 (Rated for Div. 2)

Wechat has new functions, and the test is started again

What sparks can applet container technology collide with IOT

09 softmax regression + loss function

微服務入門:Gateway網關
随机推荐
[go basics] 1 - go go
Put a lantern on the website during the Lantern Festival
Unity-Text上标平方表示形式+text判断文本是否为空
Cannot click button when method is running - C #
1. Kalman filter - the best linear filter
MySQL relearn 1-centos install mysql5.7
C # implements a queue in which everything can be sorted
go-zero微服务实战系列(九、极致优化秒杀性能)
[test de performance] lire jmeter
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)
Need help resetting PHP counters - PHP
2022 tower crane driver examination and tower crane driver examination questions and analysis
Newh3c - routing protocol (RIP, OSPF)
How to use C language code to realize the addition and subtraction of complex numbers and output structure
Sports [running 01] a programmer's half horse challenge: preparation before running + adjustment during running + recovery after running (experience sharing)
How does Xiaobai buy a suitable notebook
What if the wireless network connection of the laptop is unavailable
2022 gas examination registration and free gas examination questions
[attack and defense world | WP] cat
User login function: simple but difficult