In this article, we mainly introduce how AWK works.

The AWK workflow can be divided into three parts:

  • The code segment executed before reading the input file (identified by the BEGIN keyword).
  • The code segment executed by the main loop while reading the input file.
  • The code segment executed after reading the input file (identified by the END keyword).

Command structure:

awk 'BEGIN{ commands } pattern{ commands } END{ commands }'

The following flowchart describes the workflow of AWK:

  • 1. Execute the BEGIN block's content via the keyword BEGIN, i.e., the content{}of the curly braces after BEGIN.
  • 2. After completing the execution of the BEGIN block, start executing the body block.
  • 3. Read in the\n records separated by newline characters.
  • 4. Divide the record into fields according to the specified field delimiter, and fill the fields,$0then it represents all fields (i.e., the content of one line),$1represents the first field,$nrepresents the nth field.
  • 5. Execute each BODY block in turn; only after the pattern section successfully matches the content of the line will the content of awk-commands be executed.
  • 6. Loop reading and executing each line until the file ends, completing the execution of the body block.
  • 7. Start the execution of the END block; the END block can output the final result.

Start Block (BEGIN)

The syntax of the start block is as follows:

BEGIN {awk-commands}

The start block is the code section executed when the program starts, and it is executed only once throughout the entire process.

In general, we can initialize some variables in the start block.

BEGIN is a keyword of AWK, so it must be uppercase.

Note:The start block part is optional; your program can be without a start block part.

Body Block (BODY)

The syntax of the body part is as follows:

/pattern/ {awk-commands}

The commands in the body part are executed once for each input line.

By default, AWK executes commands for each input line. However, we can limit it to a specified pattern.

Note: there is no keyword in the body block part.

End Block (END)

The syntax of the end block is as follows:

END {awk-commands}

The end block is the code executed when the program ends. END is also a keyword of AWK, and it must also be uppercase. Like the start block, the end block is also optional.

Example

First create a file named marks.txt. It contains serial numbers, student names, course names, and obtained scores.

1)    张三    语文    80
2)    李四    数学    90
3)    王五    英语    87

Next, we will use an AWK script to display the contents of the output file, while also printing header information.

$ awk 'BEGIN{printf "序号\t名字\t课程\t分数\n"} {print}' marks.txt

Execute the above command, and the output is as follows:

序号    名字    课程    分数
1)    张三    语文    80
2)    李四    数学    90
3)    王五    英语    87

When the program starts executing, AWK outputs the header information in the start block. In the body block, each time AWK reads a line, it outputs the read content to the standard output stream until the entire file has been fully read.