awk series Part9: How to use a special mode of awk BEGIN and END

awk series: How to use a special mode of awk BEGIN and END

VIII in awk series, we introduce some powerful awk command functions, which are variable, numeric expressions and assignment operator.

In this section we will learn more about awk function, that is a special mode of awk: BEGINand END.

awk series Part9: How to use a special mode of awk BEGIN and END

Learning awk patterns BEGIN and END

As we gradually expand and explore more ways to build complex operation awk, awk will demonstrate that these special features of how powerful yes.

Before you begin, let us review describes awk series, I remember when we started this series, I pointed out that universal grammar instruction awk is this:

# awk 'script' filenames  

In the above syntax, awk script has the form:

/pattern/ { actions } 

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

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

If you use a special mode in awk script: BEGINand ENDthe following are their corresponding meanings:

  • BEGINMode: means awk performed immediately prior to input line read any BEGINaction specified.
  • ENDPattern: awk refers to the execution before it officially withdraw ENDspecified in the action.

Awk command script execution flow containing these special modes are as follows:

  1. When used in a script BEGINmode, BEGINall the action will be executed before reading any input line.
  2. Then, an input line is read and parsed into different segments.
  3. Subsequently, each non-designated special mode are compared and a match input line, when the matching is successful, it will execute the operation corresponding to the mode. Repeat this for all of your specified mode to perform this step.
  4. The next Repeat steps 2 and 3 for all input lines.
  5. When the read and process all input lines, if you specify a ENDmode, it will perform a corresponding action.

When you use a special mode, you want to get the best results in awk operation, you should remember the order of execution of the above.

For ease of understanding, let's use the example of Section VIII of demonstration, that example is about Tecmint has a list of domain names, and save it in a file called domains.txt in.

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

awk series Part9: How to use a special mode of awk BEGIN and END

View the file contents

In this example, we want to document the domain name statistics domains.txt tecmint.comnumber of occurrences. So, we wrote a simple shell script to help us complete the task, it uses variables, mathematical expressions and assignment operator of thinking, the script reads 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

Now let us apply these two special modes awk command in the above script as follows: BEGINand END:

We should put the script:

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

Read:

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

After modifying the awk command is now complete shell script like this:

#!/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

awk series Part9: How to use a special mode of awk BEGIN and END

Mode BEGIN and END awk

When we run the above script, it will be the first location domains.txt output file, and then execute the command awk script, the script commands in the special mode BEGINwill help us to output this message before reading any row from the file " 文件中出现 tecmint.com 的次数是:" .

Next, we model /^tecmint.com/will be carried out in each input line in comparison, the corresponding action { counter+=1 ; }will be performed on each line of a successful match, it will count the tecmint.comnumber of occurrences in the file.

Finally, the ENDmodel domain will output tecmint.comthe total number of occurrences in the document.

$ ./script.sh ~/domains.txt 

awk series Part9: How to use a special mode of awk BEGIN and END

Script statistics for the number of times a string appear

To sum up, we demonstrate more awk features in this section, and learn the special modes BEGINand ENDconcepts.

As I said before, these functions awk will help us build a more complex text filtering operation. Section X will give more awk features, we will learn awk built-in variable thoughts, so stay tuned.


via: http://www.tecmint.com/learn-use-awk-special-patterns-begin-and-end/

Author: Aaron Kili
Translator: ChrisLeeGit
proofread: wxy

This article from the LCTT original compiler, Linux China is proud

Reproduced in: https: //blog.51cto.com/wutengfei/2402054

Guess you like

Origin blog.csdn.net/weixin_33895604/article/details/91774784
awk