Home> System Tutorial> LINUX> body text

How to use awk's special modes BEGIN and END for processing

WBOY
Release: 2024-01-02 18:06:07
forward
763 people have browsed it

In the eighth section of the awk series, we introduced some powerful awk command functions, which are variables, numeric expressions, and assignment operators.

In this section we will learn more about awk functions, namely awk's special modes:BEGINandEND.

As we progress and explore more ways to build complex awk operations, we will prove how powerful these special features of awk are.

Before we start, let us review the introduction of the awk series. Remember when we started this series, I pointed out that the general syntax of the awk instruction is like this:

# awk 'script' filenames
Copy after login

In the above syntax, the awk script has this form:

/pattern/ { actions }
Copy after login

You will usually find that the pattern in the script (/pattern) is a regular expression. In addition, you can also use the special patterns hereBENGINandEND. Therefore, we can also write an awk command in the following form:

awk ' BEGIN { actions } /pattern/ { actions } /pattern/ { actions } ………. END { actions } ' filenames
Copy after login

If you use special modes in the awk script:BEGINandEND, the following are their corresponding meanings:

  • BEGINMode: means that awk will perform the action specified inBEGINimmediately before reading any input lines.
  • ENDMode: means that awk will perform the action specified inENDbefore it officially exits.

The execution flow of awk command scripts containing these special modes is as follows:

  1. When theBEGINmode is used in a script, all actions inBEGINwill be executed before any input lines are read.
  2. Then, an input line is read in and parsed into different segments.
  3. Next, each specified non-special pattern will be compared and matched with the input line. When the match is successful, the action corresponding to the pattern will be executed. Repeat this step for all patterns you specify.
  4. Next, repeat steps 2 and 3 for all input lines.
  5. After all input lines have been read and processed, if you specify theENDmode, the corresponding action will be performed.

When you use special modes, to get the best results from awk operations, you should remember the above execution order.

For ease of understanding, let us use the example in Section 8 for demonstration. That example is about the list of domain names owned by Tecmint and saved in a file called domains.txt.

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
Copy after login
$ cat ~/domains.txt
Copy after login

如何使用 awk 的特殊模式 BEGIN 与 END

View file content

In this example, we hope to count the number of times the domain nametecmint.comappears in the domains.txt file. So, we wrote a simple shell script to help us complete the task, which uses the ideas of variables, mathematical expressions and assignment operators. The content of the script is as follows:

#!/bin/bash for file in $@; do if [ -f $file ] ; then ### 输出文件名 echo "File is: $file" ### 输出一个递增的数字记录包含 tecmint.com 的行数 awk '/^tecmint.com/ { counter+=1 ; printf "%s/n", counter ; }' $file else ### 若输入不是文件,则输出错误信息 echo "$file 不是一个文件,请指定一个文件。" >&2 && exit 1 fi done ### 成功执行后使用退出代码 0 终止脚本 exit 0
Copy after login

Now let us apply these two special patterns in the awk command of the above script like below:BEGINandEND:

We should put the script:

awk '/^tecmint.com/ { counter+=1 ; printf "%s/n", counter ; }' $file
Copy after login

changed to:

awk ' BEGIN { print "文件中出现 tecmint.com 的次数是:" ; } /^tecmint.com/ { counter+=1 ; } END { printf "%s/n", counter ; } ' $file
Copy after login

在修改了 awk 命令之后,现在完整的 shell 脚本就像下面这样:

#!/bin/bash for file in $@; do if [ -f $file ] ; then ### 输出文件名 echo "File is: $file" ### 输出文件中 tecmint.com 出现的总次数 awk ' BEGIN { print "文件中出现 tecmint.com 的次数是:" ; } /^tecmint.com/ { counter+=1 ; } END { printf "%s/n", counter ; } ' $file else ### 若输入不是文件,则输出错误信息 echo "$file 不是一个文件,请指定一个文件。" >&2 && exit 1 fi done ### 成功执行后使用退出代码 0 终止脚本 exit 0
Copy after login

如何使用 awk 的特殊模式 BEGIN 与 END

awk 模式 BEGIN 和 END

当我们运行上面的脚本时,它会首先输出 domains.txt 文件的位置,然后执行 awk 命令脚本,该命令脚本中的特殊模式BEGIN将会在从文件读取任何行之前帮助我们输出这样的消息“文件中出现 tecmint.com 的次数是:”。

接下来,我们的模式/^tecmint.com/会在每个输入行中进行比较,对应的动作{ counter+=1 ; }会在每个匹配成功的行上执行,它会统计出tecmint.com在文件中出现的次数。

最终,END模式将会输出域名tecmint.com在文件中出现的总次数。

$ ./script.sh ~/domains.txt
Copy after login

如何使用 awk 的特殊模式 BEGIN 与 END

用于统计字符串出现次数的脚本

最后总结一下,我们在本节中演示了更多的 awk 功能,并学习了特殊模式BEGINEND的概念。

正如我之前所言,这些 awk 功能将会帮助我们构建出更复杂的文本过滤操作。第十节将会给出更多的 awk 功能,我们将会学习 awk 内置变量的思想,所以,请继续保持关注。


The above is the detailed content of How to use awk's special modes BEGIN and END for processing. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!