Easy Tutorial
❮ Android Tutorial Xfermode Porterduff4 Csharp Init Variable ❯

AWK Working Principle

Category Programming Techniques

This article primarily introduces how AWK works.

The AWK workflow can be divided into three parts:

Command structure:

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

The following flowchart describes the AWK workflow:

BEGIN Block

The syntax for the BEGIN block is as follows:

BEGIN {awk-commands}

The BEGIN block is the code segment executed at the program's startup and is executed only once throughout the process.

Typically, we can initialize some variables in the BEGIN block.

BEGIN is an AWK keyword and must be capitalized.

Note: The BEGIN block is optional; your program may not have a BEGIN block.

BODY Block

The syntax for the BODY block is as follows:

/pattern/ {awk-commands}

The commands in the BODY block are executed once for each input line.

By default, AWK executes commands for every input line. However, we can restrict this to specific patterns.

Note: There is no keyword in the BODY block.

END Block

The syntax for the END block is as follows:

END {awk-commands}

The END block is the code executed at the program's end. END is also an AWK keyword and must be capitalized. Similar to the BEGIN block, the END block is optional.

Example

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

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

Next, we will use an AWK script to display the content of the file and output the header information.

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

Executing the above command, the output is as follows:

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

When the program starts, AWK outputs the header information in the BEGIN block. In the BODY block, AWK reads each line and outputs the content to the standard output stream until the entire file is read.

❮ Android Tutorial Xfermode Porterduff4 Csharp Init Variable ❯