AWK - Workflow

This chapter will explain how exactly AWK works. To become an expert AWK programmer, one needs to know its internals. AWK follows a simple workflow: Read, Execute, and Repeat. The following diagram depicts the workflow.


Read

AWK reads a line from the input stream (file, pipe, or stdin) and stores it in memory.

Execute

All AWK commands are applied sequentially on the input. By default AWK execute commands on every line but we can restrict this by providing patterns.

Repeat

This process repeats until the file is exhausted.

Program Structure

We have seen the workflow of the AWK program. Now let us understand the structure of the AKW program.

BEGIN block

Following is the syntax of the BEGIN block:
BEGIN {awk-commands}
The begin block gets executed at program startup and is executes only once. This is good place to initialise variables. BEGIN is the AWK keyword and hence it must be in upper case. Please note that this block is optional.

Body Block

Following is the syntax of the Body block:
/pattern/ {awk-commands}
The body block apply AWK commands on every input line. By default AWK execute commands on every line but we can restrict this by providing pattern. Note that there is no keyword for Body block

END Block

Following is the syntax of the END block:
END {awk-commands}
The end block gets executed at the end of program. END is the AWK keyword and hence it must be in upper case. Please note that this block is also optional.

Examples

Let us create a file marks.txt which contains the serial number, name of the student, subject name and number of marks obtained.
1)    Amit     Physics    80
2)    Rahul    Maths      90
3)    Shyam    Biology    87
4)    Kedar    English    85
5)    Hari     History    89
Now let us display the file contents with header by using AWK script.
[jerry]$ awk 'BEGIN{printf "Sr No\tName\tSub\tMarks\n"} {print}' marks.txt
When the above code is executed, it will produce the following result.
Sr No Name     Sub        Marks
1)    Amit     Physics    80
2)    Rahul    Maths      90
3)    Shyam    Biology    87
4)    Kedar    English    85
5)    Hari     History    89
At program startup AWK prints header from BEGIN block. Then in body block, it reads a line from a file and executes AWK's print command which just prints the contents on the standard output stream. This process repeats until file is exhausted.

No comments:

Post a Comment