done translating awk part-12&13

This commit is contained in:
wenchunyang 2016-09-20 20:09:22 +08:00
parent 7cbb769e15
commit 4520c57155
4 changed files with 406 additions and 408 deletions

View File

@ -1,161 +0,0 @@
chunyang-wen doing
Part 13 - How to Write Scripts Using Awk Programming Language
====
All along from the beginning of the Awk series up to Part 12, we have been writing small Awk commands and programs on the command line and in shell scripts respectively.
However, Awk, just as Shell, is also an interpreted language, therefore, with all that we have walked through from the start of this series, you can now write Awk executable scripts.
Similar to how we write a shell script, Awk scripts start with the line:
```
#! /path/to/awk/utility -f
```
For example on my system, the Awk utility is located in /usr/bin/awk, therefore, I would start an Awk script as follows:
```
#! /usr/bin/awk -f
```
Explaining the line above:
```
#! referred to as Shebang, which specifies an interpreter for the instructions in a script
/usr/bin/awk is the interpreter
-f interpreter option, used to read a program file
```
That said, let us now dive into looking at some examples of Awk executable scripts, we can start with the simple script below. Use your favorite editor to open a new file as follows:
```
$ vi script.awk
```
And paste the code below in the file:
```
#!/usr/bin/awk -f
BEGIN { printf "%s\n","Writing my first Awk executable script!" }
```
Save the file and exit, then make the script executable by issuing the command below:
```
$ chmod +x script.awk
```
Thereafter, run it:
```
$ ./script.awk
```
Sample Output
```
Writing my first Awk executable script!
```
A critical programmer out there must be asking, “where are the comments?”, yes, you can also include comments in your Awk script. Writing comments in your code is always a good programming practice.
It helps other programmers looking through your code to understand what you are trying to achieve in each section of a script or program file.
Therefore, you can include comments in the script above as follows.
```
#!/usr/bin/awk -f
#This is how to write a comment in Awk
#using the BEGIN special pattern to print a sentence
BEGIN { printf "%s\n","Writing my first Awk executable script!" }
```
Next, we shall look at an example where we read input from a file. We want to search for a system user named aaronkilik in the account file, /etc/passwd, then print the username, user ID and user GID as follows:
Below is the content of our script called second.awk.
```
#! /usr/bin/awk -f
#use BEGIN sepecial character to set FS built-in variable
BEGIN { FS=":" }
#search for username: aaronkilik and print account details
/aaronkilik/ { print "Username :",$1,"User ID :",$3,"User GID :",$4 }
```
Save the file and exit, make the script executable and execute it as below:
```
$ chmod +x second.awk
$ ./second.awk /etc/passwd
```
Sample Output
```
Username : aaronkilik User ID : 1000 User GID : 1000
```
In the last example below, we shall use do while statement to print out numbers from 0-10:
Below is the content of our script called do.awk.
```
#! /usr/bin/awk -f
#printing from 0-10 using a do while statement
#do while statement
BEGIN {
#initialize a counter
x=0
do {
print x;
x+=1;
}
while(x<=10)
}
```
After saving the file, make the script executable as we have done before. Afterwards, run it:
```
$ chmod +x do.awk
$ ./do.awk
```
Sample Output
```
0
1
2
3
4
5
6
7
8
9
10
```
### Summary
We have come to the end of this interesting Awk series, I hope you have learned a lot from all the 13 parts, as an introduction to Awk programming language.
As I mentioned from the beginning, Awk is a complete text processing language, for that reason, you can learn more other aspects of Awk programming language such as environmental variables, arrays, functions (built-in & user defined) and beyond.
There is yet additional parts of Awk programming to learn and master, so, below, I have provided some links to important online resources that you can use to expand your Awk programming skills, these are not necessarily all that you need, you can also look out for useful Awk programming books.
For any thoughts you wish to share or questions, use the comment form below. Remember to always stay connected to Tecmint for more exciting series.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/write-shell-scripts-in-awk-programming/
作者:[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

@ -1,247 +0,0 @@
chunyang-wen doing
How to Use Flow Control Statements in Awk - part12
====
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/
作者:[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,159 @@
如何使用 Awk 语言写脚本 - Part 13
====
从 Awk 系列开始直到第 12 部分,我们都是在命令行或者脚本文件写一些简短的 Awk 命令和程序。
然而 Awk 和 Shell 一样也是一个解释语言。通过从开始到现在的一系列的学习,你现在能写可以执行的 Awk 脚本了。
和写 shell 脚本差不多Awk 脚本以下面这一行开头:
```
#! /path/to/awk/utility -f
```
例如在我的系统上Awk 工具安装在 /user/bin/awk 目录,所以我的 Awk 脚本以如下内容作为开头:
```
#! /usr/bin/awk -f
```
上面一行的解释如下:
```
#! 称为 Shebang指明使用那个解释器来执行脚本中的命令
/usr/bin/awk –解释器
-f 解释器选项,用来指定读取的程序文件
```
说是这么说,现在从下面的简单例子开始,让我们深入研究一些可执行的 Awk 脚本。使用你最喜欢的编辑器创建一个新文件,像下面这样:
```
$ vi script.awk
```
然后把下面代码粘贴到文件中:
```
#!/usr/bin/awk -f
BEGIN { printf "%s\n","Writing my first Awk executable script!" }
```
保存文件后退出,然后执行下面命令,使得脚本可执行:
```
$ chmod +x script.awk
```
然后,执行它:
```
$ ./script.awk
```
输出样例:
```
Writing my first Awk executable script!
```
一个严格的程序员一定会问:“注释呢?”。是的,你可以在 Awk 脚本中包含注释。在代码中写注释是一种良好的编程习惯。
它有利于其它程序员阅读你的代码,理解程序文件或者脚本中每一部分的功能。
所以,你可以像下面这样在脚本中增加注释:
```
#!/usr/bin/awk -f
#This is how to write a comment in Awk
#using the BEGIN special pattern to print a sentence
BEGIN { printf "%s\n","Writing my first Awk executable script!" }
```
接下来我们看一个读文件的例子。我们想从帐号文件 /etc/passwd 中查找一个叫 aaronkilik 的用户,然后像下面这样打印用户名,用户的 ID用户的 GID (译者注:组 ID)
下面是我们脚本文件的内容,文件名为 second.awk。
```
#! /usr/bin/awk -f
#use BEGIN sepecial character to set FS built-in variable
BEGIN { FS=":" }
#search for username: aaronkilik and print account details
/aaronkilik/ { print "Username :",$1,"User ID :",$3,"User GID :",$4 }
```
保存文件后退出,使得脚本可执行,然后像下面这样执行它:
```
$ chmod +x second.awk
$ ./second.awk /etc/passwd
```
输出样例
```
Username : aaronkilik User ID : 1000 User GID : 1000
```
在下面最后一个例子中,我们将使用 do while 语句来打印数字 0-10
下面是我们脚本文件的内容,文件名为 do.awk。
```
#! /usr/bin/awk -f
#printing from 0-10 using a do while statement
#do while statement
BEGIN {
#initialize a counter
x=0
do {
print x;
x+=1;
}
while(x<=10)
}
```
保存文件后,像之前操作一样使得脚本可执行。然后,运行它:
```
$ chmod +x do.awk
$ ./do.awk
```
输出样例
```
0
1
2
3
4
5
6
7
8
9
10
```
### 总结
我们已经到达这个精彩的 Awk 系列的最后,我希望你从整个 13 部分中学到了很多知识,把这些当作你 Awk 编程语言的入门指导。
我一开始就提到过Awk 是一个完整的文本处理语言,所以你可以学习很多 Awk 编程语言的其它方面,例如环境变量,数组,函数(内置的或者用户自定义的),等等。
Awk 编程还有其它内容需要学习和掌握,所以在文末我提供了一些重要的在线资源的链接,你可以利用他们拓展你的 Awk 编程技能。但这不是必须的,你也可以阅读一些关于 Awk 的书籍。
如果你任何想要分享的想法或者问题,在下面留言。记得保持关注 Tecmint会有更多的精彩内容。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/write-shell-scripts-in-awk-programming/
作者:[Aaron Kili |][a]
译者:[chunyang-wen](https://github.com/chunyang-wen)
校对:[校对者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,247 @@
如何使用 Awk 中的流控制语句 - part12
====
回顾从 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-elseforwhiledo-while 语句。关于如何使用 next 语句,如果你们记得的话,我们已经在 Awk 系列的第6部分介绍过了。
### 1. if-else 语句
if 语句的语法和 shell 里面的 if 语句类似:
```
if (condition1) {
actions1
}
else {
actions2
}
```
上面的语法中condition1 和 condition2 是 Awk 的表达式actions1 和 actions2 是当相应的条件满足时执行的 Awk 命令。
当 condition1 满足时,意味着它的值是 true此时会执行 actions1if 语句退出否则译注condition1 为 false执行 actions2。
if 语句可以扩展成如下的 if-else_if-else
```
if (condition1){
actions1
}
else if (conditions2){
actions2
}
else{
actions3
}
```
上面例子中,如果 condition1 为 true执行 actions1if 语句退出;否则对 condition2 求值,如果值为 true那么执行 actions2if 语句退出。然而如果 condition2 是 false那么会执行 actions3 退出 if语句。
下面是一个使用 if 语句的例子我们有一个存储用户和他们年龄列表的文件users.txt。
我们想要打印用户的名字以及他们的年龄是大于 25 还是小于 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 语句十分合适,它的语法如下:
这里只是简单的定义一个计数器来控制循环的执行。首先你要初始化那个计数器 counter然后根据某个条件判断是否执行如果该条件为 true 则执行,最后增加计数器。当计数器不满足条件时则终止循环。
```
for ( counter-initialization; test-condition; counter-increment ){
actions
}
```
下面的 Awk 命令利用打印数字 0-10 来说明 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 ( condition ) {
actions
}
```
上面的 condition 是 Awk 表达式actions 是当 condition 为 true 时执行的 Awk命令。
下面是仍然用打印数字 0-10 来解释 while 语句的用法:
```
#!/bin/bash
awk ' BEGIN{ counter=0 ;
while(counter<=10){
print counter;
counter+=1 ;
}
}
```
保存文件,让文件可执行,然后执行:
```
$ chmod +x test.sh
$ ./test.sh
```
输出样例
Sample Output
```
0
1
2
3
4
5
6
7
8
9
10
```
### 4. do-while 语句
这个是上面的 while 语句语法的一个变化,其语法如下:
```
do {
actions
}
while (condition)
```
二者的区别是,在 do-while 中Awk 的命令在条件求值前先执行。我们使用 while 语句中同样的例子来解释 do-while 的使用,将 test.sh 脚本中的 Awk 命令做如下更改:
```
#!/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/
作者:[Aaron Kili][a]
译者:[chunyang-wen](https://github.com/chunyang-wen)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: http://www.tecmint.com/author/aaronkili/