Awk is a very popular text processing tool. Awk provides different functionalities and structures like programming languages. if - if else
is a very useful conditional statement used to create decision trees. if - if else
used to check the given situation or operations and run accordingly. For example, we can check the age of the person and act accordingly if over 60 or below 60.
Awk是一种非常流行的文本处理工具。 Awk提供了不同的功能和结构,例如编程语言。 if - if else
是用于创建决策树的非常有用的条件语句。 if - if else
用于检查给定的情况或操作并相应运行。 例如,我们可以检查人员的年龄并在60岁以上或60岁以下时采取相应的措施。
示例数据 (Example Data)
During learning awk if- if else statements we will use following file.
在学习awk if- if else语句期间,我们将使用以下文件。
ismail 33 male
ahmet 5 male
elif 8 female
Awk If语句 (Awk If Statement)
Awk if statement syntax is like below. We will check the condition and if the condition is met or true we will execute the code part. If not we simply skip if code block.
awk if语句语法如下。 我们将检查条件,如果条件满足或为真,我们将执行代码部分。 如果没有,我们只是跳过代码块。
if (CONDITION)
{
CODE
}
In this example we will look for the name ismail
and then print his age.
在此示例中,我们将查找名称ismail
,然后打印其年龄。
{
if($1 =="ismail")
{
print $2;
print $3
}
}
We will save the awk script as if-program
and call with awk -f
parameter like below.
我们将awk脚本另存为if-program
并使用awk -f
参数调用,如下所示。
$ awk -f if-program data.txt

Or we can provide the script from bash like below.
或者我们可以从bash中提供脚本,如下所示。

Awk其他If语句(Awk Else If Statement)
What will happen if we have more than a single condition to check and execute the script. We can use else if
statements for multi-condition situations. Syntax of else if
is like below.
如果我们有多个条件来检查和执行脚本,将会发生什么情况。 对于多条件情况,我们可以使用else if
语句。 else if
语法如下。
if (CONDITION)
{
CODE
}
else if (CONDITION)
{
CODE
}
...
We can use more than if else
according to our needs. In this example, we will list persons with two different conditions like over 18 and below 18.
我们可以根据需要使用更多的if else
。 在此示例中,我们将列出具有两种不同条件的人,例如18岁以上和18岁以下。
{
if($2 > 17)
{
print $1" is adult"
}
else if($2 < 18)
{
print $1" is infant"
}
}
When we run this code like below.
当我们运行如下代码时。
$ awk -f else-if-program data.txt

Awk其他声明(Awk Else Statement)
While using if-else conditional statements we may need to specify default condition where it will meet if none of the other conditions is met. We can it else and put it to the end of the if-else if block. The syntax is like below
在使用if-else条件语句时,我们可能需要指定默认条件,如果不满足其他条件,它将在该条件下满足。 我们可以将其放置在if-else if块的末尾。 语法如下
if (CONDITION)
{
CODE
}
else if (CONDITION)
{
CODE
}
...
else
{
CODE
}
In this example we will check the gender and given record is not male we will print a message. In this example, we do not add else-if because we do not need but they can be added without a problem if needed.
在此示例中,我们将检查性别,如果给定的记录不是男性,我们将打印一条消息。 在此示例中,我们不添加else-if,因为我们不需要,但是可以根据需要添加它们,而不会出现问题。
{
if($3 == "male")
{
print $1" is male"
}
else
{
print $1" not male"
}
}
We save this script as else-program
and run like below.
我们将此脚本另存为else-program
并按以下方式运行。
$ awk -f else-program data.txt

翻译自: https://www.poftut.com/awk-if-if-else-else-statement-or-conditional-statements/