在 awk 中使用 if else 语句

AWK 是一种强大的脚本语言,它融入了 bash 壳。

它用途广泛,可用于编写各种数据提取脚本。

条件语句是任何编程或脚本语言不可或缺的一部分,AWK 也不例外。

在本教程中,我将展示在 AWK 中使用 if-else 语句的示例。 这是我的示例所依据的示例数据。

ID姓名年龄主题
111法尔汉18开发运维
89亚历克斯21安全运营
92罗恩22
100罗伯特23商业
102萨曼莎20Cloud-行政
105鲍勃21数学

我把它存放在 students.txt. 如果您想自己尝试这些示例,可以下载它。

样本数据文件 包含学生 ID、姓名、年龄和科目的样本数据文件 students.txt 349 字节

下载圈

1. awk 中的 if 语句

if 语句检查条件是真还是假。 如果条件为真,则执行语句。

这是 awk 中 if 语句的简单语法:

awk '{if (condition) {statement} }' [input_file]

现在,让我们使用students.txt 文件的样本数据,并在AWK 中使用if 条件打印ID 为100 的学生的详细信息。

awk '{                                        

if ($1=="100")

  {
    print "............... n";
    print "Name : " ,$2;
    print "Age : ",$3;
    print "Department : " ,$4;
  }
  
}' students.txt

以下是上述 awk 命令的执行:

上面的整个命令也可以这样写成一句话:

awk '{ if ($1=="100")  { print "............... n"; print "Name : " ,$2; print "Age : ",$3; print "Department : " ,$4; } }' students.txt

但是第一个更容易阅读和理解。

2. awk 中的 if else 语句

在上一个 example,只有一个条件和一个动作。 这 if else 声明与 if 陈述。

的一般格式 if else awk 中的语句是:

awk '{
  if (condition) 
    {command1} 
  else 
    {command2}
}' [input_file]

这里,如果条件为真,则执行command1,如果条件为假,则执行else部分的command2。

让我们再次采取 student.txt 数据文件。

假设您想获取所有年龄小于或等于 20 岁的学生的姓名和所在部门。

AWK 命令可能是这样的:

awk '{

if ($3<20)
  {
    print "Student "$2,"of department", $4, "is less than 20 years old"
  } 
else
  {
    print "Student "$2,"of department", $4, "is more than 20 years old"
  }

这是if else的执行和输出 example:

如您所见,只有两名学生年龄小于 20 岁。 其余学生都超过20岁。

但是上面有个问题 example. 它还将表格的描述视为真实数据,并告诉我们年龄年龄的学生姓名年龄超过20岁。

让我们用 if else-if 语句来纠正这个问题。

3. awk 中的 if else-if 语句

使用 if else if 语句,awk 命令检查多个条件。

如果第一个条件为假,则检查第二个条件。 如果第二个条件也为假,那么它检查第三个条件,依此类推。 它检查所有条件,直到其中一个条件为真。 如果不满足条件,则执行 else 部分。

if(condition1){

command1

}

else if(condition2) {

command2

}

else if(condition3) {

command3

}

.

.

.

else{

commandN

}

在上一个 example,输出还考虑了数据的第一行,这只是描述。

让我们设置一个条件来检查年龄是否为数字。 我正在使用正则表达式 /[0-9]+$/ 这意味着数值。 ~ 表示参数有一个数字。 而他们前面的否定运算符意味着该字段不应该有数字。

awk '{

if (! ($3 ~ /[0-9]+$/))
  {
    print "Age is just a number but you do not have a number"
  } 
else if ($3<20)
  {
    print "Student "$2,"of department", $4, "is less than 20 years old"
  } 
else
  {
    print "Student "$2,"of department", $4, "is more than 20 years old"
  }

}' students.txt

让我们运行它并关注输出的第一行。

如果 否则 如果 example 用 awk 命令

额外提示:使用 awk 程序文件

如果你发现直接在终端中编写长 awk 程序很麻烦,请使用 awk 文件。

创建一个新文件并命名 example.awk 或任何你想要的。 扩展在这里无关紧要。 它只是让人们很容易理解该文件是一个 awk 程序文件。

你把内容放在 '' 在上面的例子中,它紧跟在 awk 之后。

{
if (! ($3 ~ /[0-9]+$/))
  {
    print "Age is just a number but you do not have a number"
  } 
else if ($3<20)
  {
    print "Student "$2,"of department", $4, "is less than 20 years old"
  } 
else
  {
    print "Student "$2,"of department", $4, "is more than 20 years old"
  }
  
}

现在,以下列方式使用这个 awk 文件:

awk -f example.awk students.txt

它将产生与您之前看到的相同的输出。

如果别的 example 带有 awk 文件

更多奖励提示:改用三元运算符

除了在 awk 中使用 if else 语句,您还可以使用三元运算符。 三元表达式是 Awk 的 if-else 语句的简写版本。

如果条件为真,则 command1 将执行; 否则,如果条件为假,则执行 command2。

(condition) ? Command1:Command2

考虑以下 example awk 命令:

awk '{print ($3 <=20)? "Age less than 20: " $2: "Age over 20: " $2}' students.txt

在这个 example $3 指的是 Age 场地。 如果年龄小于或等于 20,那么它将打印显示“Age is less than 20”的第一个命令。 如果第一部分为 False,则执行第二个命令“Age is over 20”。

[email protected]:~$ awk '{print ($3 <=20)? "Age less than 20: " $2: "Age over 20: " $2}' students.txt
Age over 20: Name
Age less than 20: Farhaan
Age over 20: Alex
Age over 20: Ronn
Age over 20: Robert
Age less than 20: Samantha
Age over 20: Bob

结论

在本指南中,您了解了不同的使用方法 if-else AWK 编程语言中的语句。 使用这些语句,您可以控制程序的流程、评估条件、从文件中提取数据等。

如果您需要更多示例,本文将对您有所帮助。