当前位置:网站首页>Awk from entry to earth (7) conditional statements
Awk from entry to earth (7) conditional statements
2022-07-04 08:46:00 【Wonderful binary】
Just like the actual programming language ,AWK It also provides the execution flow of conditional statement control program . In this chapter, we will introduce AWK How to use conditional statements in .
IF sentence
Conditional statements test conditions, and then select and execute corresponding actions according to the conditions . The following is the syntax of conditional statements :
if (condition)
action
Curly braces can also be used to perform a set of operations :
if (condition)
{
action-1
action-1
.
.
action-n
}
The following example determines whether the number is odd or even :
$ awk 'BEGIN {num = 10; if (num % 2 == 0) printf "%d is even number.\n", num }'
Execute the above command to get the following result :
10 is even number.
IF - ELSE sentence
if-else Statement allows another set of actions to be performed when the condition is false . The following is if-else The grammar of :
if (condition)
action-1
else
action-2
among , When the condition is true action-1, Execute if condition is false action-2. Here is an example of using this statement to determine whether a number is even :
$ awk 'BEGIN {num = 11;
if (num % 2 == 0) printf "%d is even number.\n", num;
else printf "%d is odd number.\n", num
}'
The following results can be obtained by performing the above operation :
11 is odd number.
if-else-if
We can easily use multiple if-else Sentence construction if-else-if So as to realize the judgment of multiple conditions . Examples are as follows :
$ awk 'BEGIN {
a=30;
if (a==10)
print "a = 10";
else if (a == 20)
print "a = 20";
else if (a == 30)
print "a = 30";
}'
Execute the above command to get the following result :
a = 30
边栏推荐
- Live in a dream, only do things you don't say
- Take you to master the formatter of visual studio code
- How to solve the problem that computers often flash
- awk从入门到入土(5)简单条件匹配
- Sort by item from the list within the list - C #
- Unity write word
- Li Kou today's question -1200 Minimum absolute difference
- awk从入土到入门(10)awk内置函数
- 1. Kalman filter - the best linear filter
- Group programming ladder race - exercise set l1-006 continuity factor
猜你喜欢
[attack and defense world | WP] cat
[error record] no matching function for call to 'cacheflush' cacheflush();)
Newh3c - routing protocol (RIP, OSPF)
Ehrlich sieve + Euler sieve + interval sieve
Openfeign service interface call
09 softmax regression + loss function
Basic operations of databases and tables ----- view data tables
随机事件的关系与运算
How does Xiaobai buy a suitable notebook
Codeforces Global Round 21(A-E)
随机推荐
Display Chinese characters according to numbers
awk从入门到入土(9)循环语句
Group programming ladder race - exercise set l2-002 linked list de duplication
HMS core helps baby bus show high-quality children's digital content to global developers
2022 tower crane driver examination and tower crane driver examination questions and analysis
09 softmax regression + loss function
manjaro安装微信
Laravel page load problem connection reset - PHP
PHP session variable passed from form - PHP
2022 gas examination registration and free gas examination questions
Leetcode topic [array] - 121 - the best time to buy and sell stocks
没有Kubernetes怎么玩Dapr?
2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
How does Xiaobai buy a suitable notebook
Getting started with microservices: gateway gateway
MySQL relearn 1-centos install mysql5.7
awk从入门到入土(4)用户自定义变量
A method for detecting outliers of data
How to solve the problem of computer jam and slow down
Redis sentinel mechanism