当前位置:网站首页>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
边栏推荐
- If the array values match each other, shuffle again - PHP
- Collections in Scala
- Parallel shift does not provide any acceleration - C #
- DM8 command line installation and database creation
- User login function: simple but difficult
- Sort by item from the list within the list - C #
- R language ggplot2 visualization: ggplot2 visualization grouping box diagram, place the legend and title of the visualization image on the top left of the image and align them to the left, in which th
- Redis 哨兵机制
- Getting started with microservices: gateway gateway
- [Chongqing Guangdong education] National Open University spring 2019 455 logistics practice reference questions
猜你喜欢

【性能測試】一文讀懂Jmeter

How to solve the problem that computers often flash

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

Snipaste convenient screenshot software, which can be copied on the screen

What if the wireless network connection of the laptop is unavailable

A method for detecting outliers of data

Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14

ctfshow web255 web 256 web257
![Leetcode topic [array] -136- numbers that appear only once](/img/6d/f2e4b812e5dd872fbeb43732d6f27f.jpg)
Leetcode topic [array] -136- numbers that appear only once

User login function: simple but difficult
随机推荐
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
ES6 summary
SQL statement view SQL Server 2005 version number
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
Redis 哨兵机制
ctfshow web255 web 256 web257
yolov5 xml数据集转换为VOC数据集
OpenFeign 服务接口调用
1. Qt入门
Put a lantern on the website during the Lantern Festival
How to solve the problem that computers often flash
[test de performance] lire jmeter
go-zero微服务实战系列(九、极致优化秒杀性能)
Unity-Text上标平方表示形式+text判断文本是否为空
埃氏筛+欧拉筛+区间筛
1. Kalman filter - the best linear filter
Li Kou today's question -1200 Minimum absolute difference
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
R language uses cforest function in Party package to build random forest based on conditional inference trees, uses varimp function to check feature importance, and uses table function to calculate co
How college students choose suitable computers