* Delete Part 6 - How to Use ‘next’ Command with Awk in Linux.md

* Add files via upload

* Create Part 6 - How to Use ‘next’ Command with Awk in Linux.md
This commit is contained in:
kokialoves 2016-06-29 16:11:16 +08:00 committed by Ezio
parent e3803263a8
commit 97bf6bf4c3
2 changed files with 76 additions and 76 deletions

View File

@ -1,76 +0,0 @@
[translating]How to Use next Command with Awk in Linux
=============================================
![](http://www.tecmint.com/wp-content/uploads/2016/06/Use-next-Command-with-Awk-in-Linux.png)
In this sixth part of Awk series, we shall look at using `next` command, which tells Awk to skip all remaining patterns and expressions that you have provided, but instead read the next input line.
The `next` command helps you to prevent executing what I would refer to as time-wasting steps in a command execution.
To understand how it works, let us consider a file called food_list.txt that looks like this:
```
Food List Items
No Item_Name Price Quantity
1 Mangoes $3.45 5
2 Apples $2.45 25
3 Pineapples $4.45 55
4 Tomatoes $3.45 25
5 Onions $1.45 15
6 Bananas $3.45 30
```
Consider running the following command that will flag food items whose quantity is less than or equal to 20 with a `(*)` sign at the end of each line:
```
# awk '$4 <= 20 { printf "%s\t%s\n", $0,"*" ; } $4 > 20 { print $0 ;} ' food_list.txt
No Item_Name Price Quantity
1 Mangoes $3.45 5 *
2 Apples $2.45 25
3 Pineapples $4.45 55
4 Tomatoes $3.45 25
5 Onions $1.45 15 *
6 Bananas $3.45 30
```
The command above actually works as follows:
- First, it checks whether the quantity, fourth field of each input line is less than or equal to 20, if a value meets that condition, it is printed and flagged with the `(*)` sign at the end using expression one: `$4 <= 20`
- Secondly, it checks if the fourth field of each input line is greater than 20, and if a line meets the condition it gets printed using expression two: `$4 > 20`
But there is one problem here, when the first expression is executed, a line that we want to flag is printed using: `{ printf "%s\t%s\n", $0,"**" ; }` and then in the same step, the second expression is also checked which becomes a time wasting factor.
So there is no need to execute the second expression, `$4 > 20` again after printing already flagged lines that have been printed using the first expression.
To deal with this problem, you have to use the `next` command as follows:
```
# awk '$4 <= 20 { printf "%s\t%s\n", $0,"*" ; next; } $4 > 20 { print $0 ;} ' food_list.txt
No Item_Name Price Quantity
1 Mangoes $3.45 5 *
2 Apples $2.45 25
3 Pineapples $4.45 55
4 Tomatoes $3.45 25
5 Onions $1.45 15 *
6 Bananas $3.45 30
```
After a single input line is printed using `$4 <= 20` `{ printf "%s\t%s\n", $0,"*" ; next ; }`, the `next` command included will help skip the second expression `$4 > 20` `{ print $0 ;}`, so execution goes to the next input line without having to waste time on checking whether the quantity is greater than 20.
The next command is very important is writing efficient commands and where necessary, you can always use to speed up the execution of a script. Prepare for the next part of the series where we shall look at using standard input (STDIN) as input for Awk.
Hope you find this how to guide helpful and you can as always put your thoughts in writing by leaving a comment in the comment section below.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/use-next-command-with-awk-in-linux/
作者:[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,76 @@
如何使用AWK的next命令
=============================================
![](http://www.tecmint.com/wp-content/uploads/2016/06/Use-next-Command-with-Awk-in-Linux.png)
在Awk 系列的第六章, 我们来看一下`next`命令 ,它告诉 Awk 跳过你所提供的表达式而是读取下一个输入行.
`next` 命令帮助你阻止运行多余的步骤.
要明白它是如何工作的, 让我们来分析一下food_list.txt它看起来像这样 :
```
Food List Items
No Item_Name Price Quantity
1 Mangoes $3.45 5
2 Apples $2.45 25
3 Pineapples $4.45 55
4 Tomatoes $3.45 25
5 Onions $1.45 15
6 Bananas $3.45 30
```
运行下面的命令它将在每个食物数量小于或者等于20的行后面标一个星号:
```
# awk '$4 <= 20 { printf "%s\t%s\n", $0,"*" ; } $4 > 20 { print $0 ;} ' food_list.txt
No Item_Name Price Quantity
1 Mangoes $3.45 5 *
2 Apples $2.45 25
3 Pineapples $4.45 55
4 Tomatoes $3.45 25
5 Onions $1.45 15 *
6 Bananas $3.45 30
```
上面的命令实际运行如下:
- 首先, 它用`$4 <= 20`表达式检查每个输入行的第四列是否小于或者等于20如果满足条件, 它将在末尾打一个星号 `(*)` .
- 接着, 它用`$4 > 20`表达式检查每个输入行的第四列是否大于20如果满足条件显示出来.
但是这里有一个问题, 当第一个表达式用`{ printf "%s\t%s\n", $0,"**" ; }`命令进行标注的时候在同样的步骤第二个表达式也进行了判断这样就浪费了时间.
因此当我们已经用第一个表达式打印标志行的时候就不在需要用第二个表达式`$4 > 20`再次打印.
要处理这个问题, 我们需要用到`next` 命令:
```
# awk '$4 <= 20 { printf "%s\t%s\n", $0,"*" ; next; } $4 > 20 { print $0 ;} ' food_list.txt
No Item_Name Price Quantity
1 Mangoes $3.45 5 *
2 Apples $2.45 25
3 Pineapples $4.45 55
4 Tomatoes $3.45 25
5 Onions $1.45 15 *
6 Bananas $3.45 30
```
当输入行用`$4 <= 20` `{ printf "%s\t%s\n", $0,"*" ; next ; }`命令打印以后,`next`命令 将跳过第二个`$4 > 20` `{ print $0 ;}`表达式, 继续判断下一个输入行,而不是浪费时间继续判断一下是不是当前输入行还大于20.
next命令在编写高效的命令脚本时候是非常重要的, 它可以很大的提高脚本速度. 下面我们准备来学习Awk的下一个系列了.
希望这篇文章对你有帮助,你可以给我们留言.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/use-next-command-with-awk-in-linux/
作者:[Aaron Kili][a]
译者:[kokialoves](https://github.com/kokialoves)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: http://www.tecmint.com/author/aaronkili/