mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
Merge pull request #4277 from ChrisLeeGit/master
完成翻译 Part 10 - Learn How to Use Awk Built-in Variables
This commit is contained in:
commit
9bdce30f13
@ -1,120 +0,0 @@
|
||||
Being translated by ChrisLeeGit
|
||||
Learn How to Use Awk Built-in Variables – Part 10
|
||||
=================================================
|
||||
|
||||
As we uncover the section of Awk features, in this part of the series, we shall walk through the concept of built-in variables in Awk. There are two types of variables you can use in Awk, these are; user-defined variables, which we covered in Part 8 and built-in variables.
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Awk-Built-in-Variables-Examples.png)
|
||||
>Awk Built in Variables Examples
|
||||
|
||||
Built-in variables have values already defined in Awk, but we can also carefully alter those values, the built-in variables include:
|
||||
|
||||
- `FILENAME` : current input file name( do not change variable name)
|
||||
- `FR` : number of the current input line (that is input line 1, 2, 3… so on, do not change variable name)
|
||||
- `NF` : number of fields in current input line (do not change variable name)
|
||||
- `OFS` : output field separator
|
||||
- `FS` : input field separator
|
||||
- `ORS` : output record separator
|
||||
- `RS` : input record separator
|
||||
|
||||
Let us proceed to illustrate the use of some of the Awk built-in variables above:
|
||||
|
||||
To read the filename of the current input file, you can use the `FILENAME` built-in variable as follows:
|
||||
|
||||
```
|
||||
$ awk ' { print FILENAME } ' ~/domains.txt
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Awk-FILENAME-Variable.png)
|
||||
>Awk FILENAME Variable
|
||||
|
||||
You will realize that, the filename is printed out for each input line, that is the default behavior of Awk when you use `FILENAME` built-in variable.
|
||||
|
||||
Using `NR` to count the number of lines (records) in an input file, remember that, it also counts the empty lines, as we shall see in the example below.
|
||||
|
||||
When we view the file domains.txt using cat command, it contains 14 lines with text and empty 2 lines:
|
||||
|
||||
```
|
||||
$ cat ~/domains.txt
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Print-Contents-of-File.png)
|
||||
>Print Contents of File
|
||||
|
||||
|
||||
```
|
||||
$ awk ' END { print "Number of records in file is: ", NR } ' ~/domains.txt
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Awk-Count-Number-of-Lines.png)
|
||||
>Awk Count Number of Lines
|
||||
|
||||
To count the number of fields in a record or line, we use the NR built-in variable as follows:
|
||||
|
||||
```
|
||||
$ cat ~/names.txt
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/List-File-Contents.png)
|
||||
>List File Contents
|
||||
|
||||
```
|
||||
$ awk '{ print "Record:",NR,"has",NF,"fields" ; }' ~/names.txt
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Awk-Count-Number-of-Fields-in-File.png)
|
||||
>Awk Count Number of Fields in File
|
||||
|
||||
Next, you can also specify an input field separator using the FS built-in variable, it defines how Awk divides input lines into fields.
|
||||
|
||||
The default value for FS is space and tab, but we can change the value of FS to any character that will instruct Awk to divide input lines accordingly.
|
||||
|
||||
There are two methods to do this:
|
||||
|
||||
- one method is to use the FS built-in variable
|
||||
- and the second is to invoke the -F Awk option
|
||||
|
||||
Consider the file /etc/passwd on a Linux system, the fields in this file are divided using the : character, so we can specify it as the new input field separator when we want to filter out certain fields as in the following examples:
|
||||
|
||||
We can use the `-F` option as follows:
|
||||
|
||||
```
|
||||
$ awk -F':' '{ print $1, $4 ;}' /etc/passwd
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Awk-Filter-Fields-in-Password-File.png)
|
||||
>Awk Filter Fields in Password File
|
||||
|
||||
Optionally, we can also take advantage of the FS built-in variable as below:
|
||||
|
||||
```
|
||||
$ awk ' BEGIN { FS=“:” ; } { print $1, $4 ; } ' /etc/passwd
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Filter-Fields-in-File-Using-Awk.png)
|
||||
>Filter Fields in File Using Awk
|
||||
|
||||
To specify an output field separator, use the OFS built-in variable, it defines how the output fields will be separated using the character we use as in the example below:
|
||||
|
||||
```
|
||||
$ awk -F':' ' BEGIN { OFS="==>" ;} { print $1, $4 ;}' /etc/passwd
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Add-Separator-to-Field-in-File.png)
|
||||
>Add Separator to Field in File
|
||||
|
||||
In this Part 10, we have explored the idea of using Awk built-in variables which come with predefined values. But we can also change these values, though, it is not recommended to do so unless you know what you are doing, with adequate understanding.
|
||||
|
||||
After this, we shall progress to cover how we can use shell variables in Awk command operations, therefore, stay connected to Tecmint.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/awk-built-in-variables-examples/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+tecmint+%28Tecmint%3A+Linux+Howto%27s+Guide%29
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[ChrisLeeGit](https://github.com/chrisleegit)
|
||||
校对:[校对ID](https://github.com/校对ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: http://www.tecmint.com/author/aaronkili/
|
@ -0,0 +1,119 @@
|
||||
awk 系列:如何使用 awk 内置变量
|
||||
=================================================
|
||||
|
||||
我们将逐渐揭开 awk 功能的神秘面纱,在本节中,我们将介绍 awk 内置(built-in)变量的概念。你可以在 awk 中使用两种类型的变量,它们是:用户自定义(user-defined)变量(我们在第八节中已经介绍了)和内置变量。
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Awk-Built-in-Variables-Examples.png)
|
||||
> awk 内置变量示例
|
||||
|
||||
awk 内置变量已经有预先定义的值了,但我们也可以谨慎地修改这些值,awk 内置变量包括:
|
||||
|
||||
- `FILENAME` : 当前输入文件名称(不要修改变量名)
|
||||
- `NR` : 当前输入行编号(是指输入行 1,2,3……等,不要改变变量名)
|
||||
- `NF` : 当前输入行段编号(不要修改变量名)
|
||||
- `OFS` : 输出段分隔符
|
||||
- `FS` : 输入段分隔符
|
||||
- `ORS` : 输出记录分隔符
|
||||
- `RS` : 输入记录分隔符
|
||||
|
||||
让我们继续演示一些使用上述 awk 内置变量的方法:
|
||||
|
||||
想要读取当前输入文件的名称,你可以使用 `FILENAME` 内置变量,如下:
|
||||
|
||||
```
|
||||
$ awk ' { print FILENAME } ' ~/domains.txt
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Awk-FILENAME-Variable.png)
|
||||
> awk FILENAME 变量
|
||||
|
||||
你会看到,每一行都会对应输出一次文件名,那是你使用 `FILENAME` 内置变量时 awk 默认的行为。
|
||||
|
||||
我们可以使用 `NR` 来统计一个输入文件的行数(记录),谨记,它也会计算空行,正如我们将要在下面的例子中看到的那样。
|
||||
|
||||
当我们使用 cat 命令查看文件 domains.txt 时,会发现它有 14 行文本和 2 个空行:
|
||||
|
||||
```
|
||||
$ cat ~/domains.txt
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Print-Contents-of-File.png)
|
||||
> 输出文件内容
|
||||
|
||||
|
||||
```
|
||||
$ awk ' END { print "文件记录数是:", NR } ' ~/domains.txt
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Awk-Count-Number-of-Lines.png)
|
||||
> awk 统计行数
|
||||
|
||||
想要统计一条记录或一行中的段数,我们可以像下面那样使用 NR 内置变量:
|
||||
|
||||
```
|
||||
$ cat ~/names.txt
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/List-File-Contents.png)
|
||||
> 列出文件内容
|
||||
|
||||
```
|
||||
$ awk '{ print "记录:",NR,"有",NF,"段" ; }' ~/names.txt
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Awk-Count-Number-of-Fields-in-File.png)
|
||||
> awk 统计文件中的段数
|
||||
|
||||
接下来,你也可以使用 FS 内置变量指定一个输入文件分隔符,它会定义 awk 如何将输入行划分成段。
|
||||
|
||||
FS 默认值为空格和 TAB,但我们也能将 FS 值修改为任何字符来让 awk 根据情况分隔输入行。
|
||||
|
||||
有两种方法可以达到目的:
|
||||
|
||||
- 第一种方法是使用 FS 内置变量
|
||||
- 第二种方法是使用 awk 的 -F 选项
|
||||
|
||||
来看 Linux 系统上的 `/etc/passwd` 文件,该文件中的各段是使用 `:` 分隔的,因此,当我们想要过滤出某些段时,可以将 `:` 指定为新的输入段分隔符,示例如下:
|
||||
|
||||
我们可以使用 `-F` 选项,如下:
|
||||
|
||||
```
|
||||
$ awk -F':' '{ print $1, $4 ;}' /etc/passwd
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Awk-Filter-Fields-in-Password-File.png)
|
||||
> awk 过滤密码文件中的各段
|
||||
|
||||
此外,我们也可以利用 FS 内置变量,如下:
|
||||
|
||||
```
|
||||
$ awk ' BEGIN { FS=“:” ; } { print $1, $4 ; } ' /etc/passwd
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Filter-Fields-in-File-Using-Awk.png)
|
||||
> 使用 awk 过滤文件中的各段
|
||||
|
||||
使用 OFS 内置变量来指定一个输出段分隔符,它会定义如何使用指定的字符分隔输出段,示例如下:
|
||||
|
||||
```
|
||||
$ awk -F':' ' BEGIN { OFS="==>" ;} { print $1, $4 ;}' /etc/passwd
|
||||
```
|
||||
|
||||
![](http://www.tecmint.com/wp-content/uploads/2016/07/Add-Separator-to-Field-in-File.png)
|
||||
> 向文件中的段添加分隔符
|
||||
|
||||
在第十节(即本节)中,我们已经学习了使用含有预定义值的 awk 内置变量的理念。但我们也能够修改这些值,虽然并不推荐这样做,除非你晓得自己在做什么,并且充分理解(这些变量值)。
|
||||
|
||||
此后,我们将继续学习如何在 awk 命令操作中使用 shell 变量,所以,请继续关注 Tecmint。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/awk-built-in-variables-examples/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+tecmint+%28Tecmint%3A+Linux+Howto%27s+Guide%29
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[ChrisLeeGit](https://github.com/chrisleegit)
|
||||
校对:[校对ID](https://github.com/校对ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: http://www.tecmint.com/author/aaronkili/
|
Loading…
Reference in New Issue
Block a user