Understanding the use of BEGIN and END in AWK

Use of begin and end in
awk awk use
grammar
awk'script' filenames 

The script in the awk syntax is composed of multiple pattern+actions,
a single /pattern/ {actions} 

Unusable patterns can achieve different functions, and frequently used patterns such as regular expressions, BEGIN, END, etc.
Multiple scripts are as follows:
awk '
     BEGIN {actions} 
     /pattern/ {actions}
     /pattern/ {actions}
            ……….
     END {actions} 
' filenames In the
 
above example, we called BEGIN, END, PATTERN/
BEGIN PATTERN: Before reading the filenames file . AWK will execute the action(s) following BEGIN PATTERN once .
END PATTERN: When the file of filename is processed by the pattern above , AWK will execute the action(s) following the END PATTERN.

Let's explain the above example
1. Before reading the filename file, execute the actions after BEGIN
2. Then read the contents of the filename line by line and perform the second and third steps of pattern matching
3. When the pattern is matched , The actions behind will be executed
4. When the second and third pattern and all lines in the filename match and are processed
5. The actions after END will be executed

Guess you like

Origin blog.csdn.net/VoiceRoom/article/details/106499254