translated

This commit is contained in:
vim-kakali 2016-08-01 02:07:10 +08:00
parent 00f9e46c99
commit 705f188aa6
2 changed files with 275 additions and 275 deletions

View File

@ -1,275 +0,0 @@
translating by vim-kakali
Learn How to Use Awk Variables, Numeric Expressions and Assignment Operators part8
=======================================================================================
The [Awk command series][1] is getting exciting I believe, in the previous seven parts, we walked through some fundamentals of Awk that you need to master to enable you perform some basic text or string filtering in Linux.
Starting with this part, we shall dive into advance areas of Awk to handle more complex text or string filtering operations. Therefore, we are going to cover Awk features such as variables, numeric expressions and assignment operators.
![](http://www.tecmint.com/wp-content/uploads/2016/07/Learn-Awk-Variables-Numeric-Expressions-Assignment-Operators.png)
>Learn Awk Variables, Numeric Expressions and Assignment Operators
These concepts are not comprehensively distinct from the ones you may have probably encountered in many programming languages before such shell, C, Python plus many others, so there is no need to worry much about this topic, we are simply revising the common ideas of using these mentioned features.
This will probably be one of the easiest Awk command sections to understand, so sit back and lets get going.
### 1. Awk Variables
In any programming language, a variable is a place holder which stores a value, when you create a variable in a program file, as the file is executed, some space is created in memory that will store the value you specify for the variable.
You can define Awk variables in the same way you define shell variables as follows:
```
variable_name=value
```
In the syntax above:
- `variable_name`: is the name you give a variable
- `value`: the value stored in the variable
Lets look at some examples below:
```
computer_name=”tecmint.com”
port_no=”22”
email=”admin@tecmint.com”
server=”computer_name”
```
Take a look at the simple examples above, in the first variable definition, the value `tecmint.com` is assigned to the variable `computer_name`.
Furthermore, the value 22 is assigned to the variable port_no, it is also possible to assign the value of one variable to another variable as in the last example where we assigned the value of computer_name to the variable server.
If you can recall, right from [part 2 of this Awk series][2] were we covered field editing, we talked about how Awk divides input lines into fields and uses standard field access operator, $ to read the different fields that have been parsed. We can also use variables to store the values of fields as follows.
```
first_name=$2
second_name=$3
```
In the examples above, the value of first_name is set to second field and second_name is set to the third field.
As an illustration, consider a file named names.txt which contains a list of an applications users indicating their first and last names plus gender. Using the [cat command][3], we can view the contents of the file as follows:
```
$ cat names.txt
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/List-File-Content-Using-cat-Command.png)
>List File Content Using cat Command
Then, we can also use the variables first_name and second_name to store the first and second names of the first user on the list as by running the Awk command below:
```
$ awk '/Aaron/{ first_name=$2 ; second_name=$3 ; print first_name, second_name ; }' names.txt
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/Store-Variables-Using-Awk-Command.png)
>Store Variables Using Awk Command
Let us also take a look at another case, when you issue the command `uname -a` on your terminal, it prints out all your system information.
The second field contains your `hostname`, therefore we can store the hostname in a variable called hostname and print it using Awk as follows:
```
$ uname -a
$ uname -a | awk '{hostname=$2 ; print hostname ; }'
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/Store-Command-Output-to-Variable-Using-Awk.png)
>Store Command Output to Variable Using Awk
### 2. Numeric Expressions
In Awk, numeric expressions are built using the following numeric operators:
- `*` : multiplication operator
- `+` : addition operator
- `/` : division operator
- `-` : subtraction operator
- `%` : modulus operator
- `^` : exponentiation operator
The syntax for a numeric expressions is:
```
$ operand1 operator operand2
```
In the form above, operand1 and operand2 can be numbers or variable names, and operator is any of the operators above.
Below are some examples to demonstrate how to build numeric expressions:
```
counter=0
num1=5
num2=10
num3=num2-num1
counter=counter+1
```
To understand the use of numeric expressions in Awk, we shall consider the following example below, with the file domains.txt which contains all domains owned by Tecmint.
```
news.tecmint.com
tecmint.com
linuxsay.com
windows.tecmint.com
tecmint.com
news.tecmint.com
tecmint.com
linuxsay.com
tecmint.com
news.tecmint.com
tecmint.com
linuxsay.com
windows.tecmint.com
tecmint.com
```
To view the contents of the file, use the command below:
```
$ cat domains.txt
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/View-Contents-of-File.png)
>View Contents of File
If we want to count the number of times the domain tecmint.com appears in the file, we can write a simple script to do that as follows:
```
#!/bin/bash
for file in $@; do
if [ -f $file ] ; then
#print out filename
echo "File is: $file"
#print a number incrementally for every line containing tecmint.com
awk '/^tecmint.com/ { counter=counter+1 ; printf "%s\n", counter ; }' $file
else
#print error info incase input is not a file
echo "$file is not a file, please specify a file." >&2 && exit 1
fi
done
#terminate script with exit code 0 in case of successful execution
exit 0
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/Shell-Script-to-Count-a-String-in-File.png)
>Shell Script to Count a String or Text in File
After creating the script, save it and make it executable, when we run it with the file, domains.txt as out input, we get the following output:
```
$ ./script.sh ~/domains.txt
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/Script-To-Count-String.png)
>Script to Count String or Text
From the output of the script, there are 6 lines in the file domains.txt which contain tecmint.com, to confirm that you can manually count them.
### 3. Assignment Operators
The last Awk feature we shall cover is assignment operators, there are several assignment operators in Awk and these include the following:
- `*=` : multiplication assignment operator
- `+=` : addition assignment operator
- `/=` : division assignment operator
- `-=` : subtraction assignment operator
- `%=` : modulus assignment operator
- `^=` : exponentiation assignment operator
The simplest syntax of an assignment operation in Awk is as follows:
```
$ variable_name=variable_name operator operand
```
Examples:
```
counter=0
counter=counter+1
num=20
num=num-1
```
You can use the assignment operators above to shorten assignment operations in Awk, consider the previous examples, we could perform the assignment in the following form:
```
variable_name operator=operand
counter=0
counter+=1
num=20
num-=1
```
Therefore, we can alter the Awk command in the shell script we just wrote above using += assignment operator as follows:
```
#!/bin/bash
for file in $@; do
if [ -f $file ] ; then
#print out filename
echo "File is: $file"
#print a number incrementally for every line containing tecmint.com
awk '/^tecmint.com/ { counter+=1 ; printf "%s\n", counter ; }' $file
else
#print error info incase input is not a file
echo "$file is not a file, please specify a file." >&2 && exit 1
fi
done
#terminate script with exit code 0 in case of successful execution
exit 0
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/Alter-Shell-Script.png)
>Alter Shell Script
In this segment of the [Awk series][4], we covered some powerful Awk features, that is variables, building numeric expressions and using assignment operators, plus some few illustrations of how we can actually use them.
These concepts are not any different from the one in other programming languages but there may be some significant distinctions under Awk programming.
In part 9, we shall look at more Awk features that is special patterns: BEGIN and END. Until then, stay connected to Tecmint.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/learn-awk-variables-numeric-expressions-and-assignment-operators/
作者:[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/
[1]: http://www.tecmint.com/category/awk-command/
[2]: http://www.tecmint.com/awk-print-fields-columns-with-space-separator/
[3]: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/
[4]: http://www.tecmint.com/category/awk-command/

View File

@ -0,0 +1,275 @@
第 8 节--学习怎样使用 Awk 变量,数值表达式以及赋值运算符
=======================================================================================
我相信 [Awk 命令系列][1] 将会令人兴奋不已,在系列的前几节我们讨论了在 Linux 中处理文件和筛选字符串需要的基本 Awk 命令。
在这一部分,我们会对处理更复杂的文件和筛选字符串操作需要的更高级的命令进行讨论。因此,我们将会看到关于 Awk 的一些特性诸如变量,数值表达式和赋值运算符。
![](http://www.tecmint.com/wp-content/uploads/2016/07/Learn-Awk-Variables-Numeric-Expressions-Assignment-Operators.png)
>学习 Awk 变量,数值表达式和赋值运算符
你可能已经在很多编程语言中接触过它们,比如 shellCPython等这些概念在理解上和这些语言没有什么不同所以在这一小节中你不用担心很难理解我们将会简短的提及常用的一些 Awk 特性。
这一小节可能是 Awk 命令里最容易理解的部分,所以放松点,我们开始吧。
### 1. Awk 变量
在任何编程语言中,当你在程序中新建一个变量的时候这个变量就是一个存储了值的占位符,程序一运行就占用了一些内存空间,你为变量赋的值会存储在这些内存空间上。
你可以像下面这样定义 shell 变量一样定义 Awk 变量:
```
variable_name=value
```
上面的语法:
- `variable_name`: 为定义的变量的名字
- `value`: 为变量赋的值
再看下面的一些例子:
```
computer_name=”tecmint.com”
port_no=”22”
email=”admin@tecmint.com”
server=”computer_name”
```
观察上面的简单的例子,在定义第一个变量的时候,值 'tecmint.com' 被赋给了 'computer_name' 变量。
此外,值 22 也被赋给了 port_no 变量,把一个变量的值赋给另一个变量也是可以的,在最后的例子中我们把变量 computer_name 的值赋给了变量 server。
你可以看看 [本系列的第 2 节][2] 中提到的字段编辑,我们讨论了 Awk 怎样将输入的行分隔为若干字段并且使用标准的字段进行输入操作 $ 访问不同的被分配的字段。我们也可以像下面这样使用变量为字段赋值。
```
first_name=$2
second_name=$3
```
在上面的例子中,变量 first_name 的值设置为第二个字段second_name 的值设置为第三个字段。
再举个例子,有一个名为 names.txt 的文件,这个文件包含了一个应用程序的用户列表,这个用户列表显示了用户的名字和曾用名以及性别。可以使用 [cat 命令][3] 查看文件内容:
```
$ cat names.txt
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/List-File-Content-Using-cat-Command.png)
>使用 cat 命令查看列表文件内容
然后,我们也可以使用下面的 Awk 命令把列表中第一个用户的第一个和第二个名字分别存储到变量 first_name 和 second_name 上:
```
$ awk '/Aaron/{ first_name=$2 ; second_name=$3 ; print first_name, second_name ; }' names.txt
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/Store-Variables-Using-Awk-Command.png)
>使用 Awk 命令为变量赋值
再看一个例子,当你在终端运行 'uname -a' 时,它可以打印出所有的系统信息。
第二个字段包含了你的 'hostname',因此,我们可以像下面这样把它赋给一个叫做 hostname 的变量并且用 Awk 打印出来。
```
$ uname -a
$ uname -a | awk '{hostname=$2 ; print hostname ; }'
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/Store-Command-Output-to-Variable-Using-Awk.png)
>使用 Awk 把命令的输出赋给变量
### 2. 数值表达式
在 Awk 中,数值表达式使用下面的数值运算符组成:
- `*` : 乘法运算符
- `+` : 加法运算符
- `/` : 除法运算符
- `-` : 减法运算符
- `%` : 取模运算符
- `^` : 指数运算符
数值表达式的语法是:
```
$ operand1 operator operand2
```
上面的 operand1 和 operand2 可以是数值和变量,运算符可以是上面列出的任意一种。
下面是一些展示怎样使用数值表达式的例子:
```
counter=0
num1=5
num2=10
num3=num2-num1
counter=counter+1
```
理解了 Awk 中数值表达式的用法,我们就可以看下面的例子了,文件 domians.txt 里包括了所有属于 Tecmint 的域名。
```
news.tecmint.com
tecmint.com
linuxsay.com
windows.tecmint.com
tecmint.com
news.tecmint.com
tecmint.com
linuxsay.com
tecmint.com
news.tecmint.com
tecmint.com
linuxsay.com
windows.tecmint.com
tecmint.com
```
可以使用下面的命令查看文件的内容;
```
$ cat domains.txt
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/View-Contents-of-File.png)
>查看文件内容
如果想要计算出域名 tecmint.com 在文件中出现的次数,我们就可以通过写一个简单的脚本实现这个功能:
```
#!/bin/bash
for file in $@; do
if [ -f $file ] ; then
#print out filename
echo "File is: $file"
#print a number incrementally for every line containing tecmint.com
awk '/^tecmint.com/ { counter=counter+1 ; printf "%s\n", counter ; }' $file
else
#print error info incase input is not a file
echo "$file is not a file, please specify a file." >&2 && exit 1
fi
done
#terminate script with exit code 0 in case of successful execution
exit 0
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/Shell-Script-to-Count-a-String-in-File.png)
>计算一个字符串或文本在文件中出现次数的 shell 脚本
写完脚本后保存并赋予执行权限,当我们使用文件运行脚本的时候,文件 domains.txt 作为脚本的输入,我们会得到下面的输出:
```
$ ./script.sh ~/domains.txt
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/Script-To-Count-String.png)
>计算字符串或文本出现次数的脚本
从脚本执行后的输出中,可以看到在文件 domains.txt 中包含域名 tecmint.com 的地方有 6 行,你可以自己计算进行验证。
### 3. 赋值操作符
我们要说的最后的 Awk 特性是赋值运算符,下面列出的只是 Awk 中的部分赋值运算符:
- `*=` : 乘法赋值运算符
- `+=` : 加法赋值运算符
- `/=` : 除法赋值运算符
- `-=` : 减法赋值运算符
- `%=` : 取模赋值运算符
- `^=` : 指数赋值运算符
下面是 Awk 中最简单的一个赋值操作的语法:
```
$ variable_name=variable_name operator operand
```
例子:
```
counter=0
counter=counter+1
num=20
num=num-1
```
你可以使用在 Awk 中使用上面的赋值操作符使命令更简短,从先前的例子中,我们可以使用下面这种格式进行赋值操作:
```
variable_name operator=operand
counter=0
counter+=1
num=20
num-=1
```
因此,我们可以在 shell 脚本中改变 Awk 命令,使用上面提到的 += 操作符:
```
#!/bin/bash
for file in $@; do
if [ -f $file ] ; then
#print out filename
echo "File is: $file"
#print a number incrementally for every line containing tecmint.com
awk '/^tecmint.com/ { counter+=1 ; printf "%s\n", counter ; }' $file
else
#print error info incase input is not a file
echo "$file is not a file, please specify a file." >&2 && exit 1
fi
done
#terminate script with exit code 0 in case of successful execution
exit 0
```
![](http://www.tecmint.com/wp-content/uploads/2016/07/Alter-Shell-Script.png)
>改变了的 shell 脚本
在 [Awk 系列][4] 的这一部分,我们讨论了一些有用的 Awk 特性,有变量,使用数值表达式和赋值运算符,还有一些使用他们的实例。
这些概念和其他的编程语言没有任何不同,但是可能在 Awk 中有一些意义上的区别。
在本系列的第 9 节,我们会学习更多的 Awk 特性,比如特殊格式: BEGIN 和 END。这也会与 Tecmit 有联系。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/learn-awk-variables-numeric-expressions-and-assignment-operators/
作者:[Aaron Kili][a]
译者:[vim-kakali](https://github.com/vim-kakali)
校对:[校对ID](https://github.com/校对ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: http://www.tecmint.com/author/aaronkili/
[1]: http://www.tecmint.com/category/awk-command/
[2]: http://www.tecmint.com/awk-print-fields-columns-with-space-separator/
[3]: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/
[4]: http://www.tecmint.com/category/awk-command/