Translated 20160805 How to Use Flow Control Statements in Awk - Part 12

This commit is contained in:
robot527 2016-08-24 20:39:00 +08:00
parent d59f1f0c23
commit a113632bb3
2 changed files with 245 additions and 261 deletions

View File

@ -1,261 +0,0 @@
robot527 translating
How to Use Flow Control Statements in Awk Part 12
===========================================
When you review all the Awk examples we have covered so far, right from the start of the Awk series, you will notice that all the commands in the various examples are executed sequentially, that is one after the other. But in certain situations, we may want to run some text filtering operations based on some conditions, that is where the approach of flow control statements sets in.
![](http://www.tecmint.com/wp-content/uploads/2016/08/Use-Flow-Control-Statements-in-Awk.png)
There are various flow control statements in Awk programming and these include:
- if-else statement
- for statement
- while statement
- do-while statement
- break statement
- continue statement
- next statement
- nextfile statement
- exit statement
However, for the scope of this series, we shall expound on: if-else, for, while and do while statements. Remember that we already walked through how to use next statement in Part 6 of this Awk series.
### 1. The if-else Statement
The expected syntax of the if statement is similar to that of the shell if statement:
```
if (condition1) {
actions1
}
else {
actions2
}
```
In the above syntax, condition1 and condition2 are Awk expressions, and actions1 and actions2 are Awk commands executed when the respective conditions are satisfied.
When condition1 is satisfied, meaning its true, then actions1 is executed and the if statement exits, otherwise actions2 is executed.
The if statement can also be expanded to a if-else_if-else statement as below:
```
if (condition1){
actions1
}
else if (conditions2){
actions2
}
else{
actions3
}
```
For the form above, if condition1 is true, then actions1 is executed and the if statement exits, otherwise condition2 is evaluated and if it is true, then actions2 is executed and the if statement exits. However, when condition2 is false then, actions3 is executed and the if statement exits.
Here is a case in point of using if statements, we have a list of users and their ages stored in the file, users.txt.
We want to print a statement indicating a users name and whether the users age is less or more than 25 years old.
```
aaronkilik@tecMint ~ $ cat users.txt
Sarah L 35 F
Aaron Kili 40 M
John Doo 20 M
Kili Seth 49 M
```
We can write a short shell script to carry out our job above, here is the content of the script:
```
#!/bin/bash
awk ' {
if ( $3 <= 25 ){
print "User",$1,$2,"is less than 25 years old." ;
}
else {
print "User",$1,$2,"is more than 25 years old" ;
}
}' ~/users.txt
```
Then save the file and exit, make the script executable and run it as follows:
```
$ chmod +x test.sh
$ ./test.sh
```
Sample Output
```
User Sarah L is more than 25 years old
User Aaron Kili is more than 25 years old
User John Doo is less than 25 years old.
User Kili Seth is more than 25 years old
```
### 2. The for Statement
In case you want to execute some Awk commands in a loop, then the for statement offers you a suitable way to do that, with the syntax below:
Here, the approach is simply defined by the use of a counter to control the loop execution, first you need to initialize the counter, then run it against a test condition, if it is true, execute the actions and finally increment the counter. The loop terminates when the counter does not satisfy the condition.
```
for ( counter-initialization; test-condition; counter-increment ){
actions
}
```
The following Awk command shows how the for statement works, where we want to print the numbers 0-10:
```
$ awk 'BEGIN{ for(counter=0;counter<=10;counter++){ print counter} }'
```
Sample Output
```
0
1
2
3
4
5
6
7
8
9
10
```
### 3. The while Statement
The conventional syntax of the while statement is as follows:
```
while ( condition ) {
actions
}
```
The condition is an Awk expression and actions are lines of Awk commands executed when the condition is true.
Below is a script to illustrate the use of while statement to print the numbers 0-10:
```
#!/bin/bash
awk ' BEGIN{ counter=0 ;
while(counter<=10){
print counter;
counter+=1 ;
}
}
```
Save the file and make the script executable, then run it:
```
$ chmod +x test.sh
$ ./test.sh
```
Sample Output
```
0
1
2
3
4
5
6
7
8
9
10
```
### 4. The do while Statement
It is a modification of the while statement above, with the following underlying syntax:
```
do {
actions
}
while (condition)
```
The slight difference is that, under do while, the Awk commands are executed before the condition is evaluated. Using the very example under while statement above, we can illustrate the use of do while by altering the Awk command in the test.sh script as follows:
```
#!/bin/bash
awk ' BEGIN{ counter=0 ;
do{
print counter;
counter+=1 ;
}
while (counter<=10)
}
'
```
After modifying the script, save the file and exit. Then make the script executable and execute it as follows:
```
$ chmod +x test.sh
$ ./test.sh
```
Sample Output
```
0
1
2
3
4
5
6
7
8
9
10
```
### Conclusion
This is not a comprehensive guide regarding Awk flow control statements, as I had mentioned earlier on, there are several other flow control statements in Awk.
Nonetheless, this part of the Awk series should give you a clear fundamental idea of how execution of Awk commands can be controlled based on certain conditions.
You can as well expound more on the rest of the flow control statements to gain more understanding on the subject matter. Finally, in the next section of the Awk series, we shall move into writing Awk scripts.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/use-flow-control-statements-with-awk-command/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+tecmint+%28Tecmint%3A+Linux+Howto%27s+Guide%29
作者:[Aaron Kili][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: http://www.tecmint.com/author/aaronkili/

View File

@ -0,0 +1,245 @@
awk 系列:在 awk 中如何使用流程控制语句
===========================================
当你回顾所有到目前为止我们已经覆盖的 awk 实例,从 awk 系列的开始,你会注意到各种实例的所有指令是顺序执行的,即一个接一个地执行。但在某些情况下,我们可能希望运行基于一些条件的文本过滤操作,即其中的流程控制语句集的方法。
![](http://www.tecmint.com/wp-content/uploads/2016/08/Use-Flow-Control-Statements-in-Awk.png)
在 awk 编程中有各种各样的流程控制语句,其中包括:
- if-else 语句
- for 语句
- while 语句
- do-while 语句
- break 语句
- continue 语句
- next 语句
- nextfile 语句
- exit 语句
然而对于该系列的这一部分我们将阐述if-else、for、while 和 do while 语句。请记住,我们已经在这个 awk 系列的第 6 部分介绍过如何使用 awk 的 next 语句。
### 1. if-else 语句
if 语句预期的语法类似于 shell 中的 if 语句:
```
if (条件 1) {
动作 1
}
else {
动作 2
}
```
在上述语法中,条件 1 和条件 2 是 awk 表达式,而动作 1 和动作 2 是当各自的条件得到满足时 awk 命令的执行。
当条件 1 满足时,意味着它为真,那么动作 1 被执行并退出 if 语句,否则动作 2 被执行。
if 语句还能扩展为如下的 if-else_if-else 语句:
```
if (条件 1){
动作 1
}
else if (条件 2){
动作 2
}
else{
动作 3
}
```
对于上面的形式,如果条件 1 为真,那么动作 1 被执行并退出 if 语句,否则条件 2 被求值且如果值为真,那么动作 2 被执行并退出 if 语句。然而,当条件 2 为假时,那么动作 3 被执行并退出 if 语句。
这是在使用 if 语句的一个实例,我们有一个用户和他们年龄的列表,存储在文件 users.txt 中。
我们要打印一个清单,显示用户的名称和用户的年龄是否小于或超过 25 岁。
```
aaronkilik@tecMint ~ $ cat users.txt
Sarah L 35 F
Aaron Kili 40 M
John Doo 20 M
Kili Seth 49 M
```
我们可以写一个简短的 shell 脚本来执行上文中我们的工作,这是脚本的内容:
```
#!/bin/bash
awk ' {
if ( $3 <= 25 ){
print "User",$1,$2,"is less than 25 years old." ;
}
else {
print "User",$1,$2,"is more than 25 years old" ;
}
}' ~/users.txt
```
然后保存文件并退出,按如下方式使脚本可执行并运行它:
```
$ chmod +x test.sh
$ ./test.sh
```
输出样例
```
User Sarah L is more than 25 years old
User Aaron Kili is more than 25 years old
User John Doo is less than 25 years old.
User Kili Seth is more than 25 years old
```
### 2. for 语句
如果你想在一个循环中执行一些 awk 命令,那么具有下面语法的 for 语句为你提供一个合适的方式来做:
这里,该方法是简单地通过一个计数器来控制循环执行的定义,首先你需要初始化这个计数器,然后针对测试条件运行它,如果它为真,执行这些动作并最终增加这个计数器。当计数器不满足条件时,循环终止。
```
for ( 计数器的初始化 ; 测试条件 ; 计数器增加 ){
动作
}
```
在我们想要打印数字 0 到 10 时,以下 awk 命令显示 for 语句是如何工作的:
```
$ awk 'BEGIN{ for(counter=0;counter<=10;counter++){ print counter} }'
```
输出样例
```
0
1
2
3
4
5
6
7
8
9
10
```
### 3. while 语句
while 语句的传统语法如下:
```
while ( 条件 ) {
动作
}
```
这条件是一个 awk 表达式而动作是当条件为真时被执行的 awk 命令。
下面是一个说明使用 while 语句来打印数字 0 到 10 的脚本:
```
#!/bin/bash
awk ' BEGIN{ counter=0;
while(counter<=10){
print counter;
counter+=1;
}
}'
```
保存文件并使脚本可执行,然后运行它:
```
$ chmod +x test.sh
$ ./test.sh
```
输出样例
```
0
1
2
3
4
5
6
7
8
9
10
```
### 4. do while 语句
它是上文中 while 语句的一个变型,具有以下语法:
```
do {
动作
}
while (条件)
```
这轻微的区别在于,在 do while 语句下awk 的命令在求值条件之前执行。使用上文 while 语句的例子,我们可以通过按如下所述修改 test.sh 脚本中的 awk 命令来说明 do while 语句的用法:
```
#!/bin/bash
awk ' BEGIN{ counter=0;
do{
print counter;
counter+=1;
}
while (counter<=10)
}'
```
修改脚本之后,保存文件并退出。按如下方式使脚本可执行并执行它:
```
$ chmod +x test.sh
$ ./test.sh
```
输出样例
```
0
1
2
3
4
5
6
7
8
9
10
```
### 总结
这不是关于 awk 的流程控制语句的一个全面的指南,正如我早先提到的,还有其他几个流程控制语句在 awk 里。
尽管如此awk 系列的这一部分使应该你明白了一个明确的基于某些条件控制的 awk 命令是如何执行的基本概念。
你还可以阐述更多其余的流程控制语句以获得更多关于该主题的理解。最后,在 awk 的系列下一节,我们将进入编写 awk 脚本。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/use-flow-control-statements-with-awk-command/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+tecmint+%28Tecmint%3A+Linux+Howto%27s+Guide%29
作者:[Aaron Kili][a]
译者:[robot527](https://github.com/robot527)
校对:[校对者 ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux 中国](https://linux.cn/) 荣誉推出
[a]: http://www.tecmint.com/author/aaronkili/