Part3: BEGIN and END Block in AWK


This is Part3 of the series of posts on awk utility.

Part1: Introduction to AWK
Part2: Regular Expressions in AWK

As awk is used for reporting, one might need options to print a header or footer to the report. BEGIN and END statements allow that.

BEGIN is used for declaring variables and printing banners while END is used for summaries and totals. The main block (which we have been using till now) is where the data stream (example: file or std input) is processed. In the main block, each line from the input is processed one by one, whereas the BEGIN and END blocks are procesed only once.

The following example shows BEGIN block being used without main or END block.

awk 'BEGIN{print "Welcome"}'
Welcome

In the above example, we did not pass any input. Let's look at how begin and main block are used

awk 'BEGIN{print "Welcome"}{print}'
Welcome
Hi this is me typing
Hi this is me typing

In the above example, the begin block printed "Welcome". The input, taken from the keyboard, is processed subsequently in the main block. As the action in the main block is just print, the input is printed as it is.

Let's add END block.

awk 'BEGIN{print "Welcome"}{print}END{print "Bye"}'
Welcome
hi
hi
1
1
2
2
3
3
^d
Bye

In example, the begin block printed the Welcome banner, the main block processed the input hi, 1, 2 and 3. After the main block is done, the end block printed Bye.

Let's use the begin and end blocks in a more useful way. In the following example, we define a variable sum in the begin block. The main block picks the 5th column from the output of ls -l (which is the size of each file). The size of the file gets added to sum until all the lines from the output of ls -l are processed. Finally, the end block prints the sum (which gives you the sum of sizes of all the files in the folder).

ls -l | awk 'BEGIN{sum=0}{sum=sum+$5}END{print sum}'
11663

Comments

Popular posts from this blog

OS/DB Migration - CMD. STR, TOC, EXT, R3load, DDLDBS.TPL and more

Fixing Inconsistent Table - Table activation fails due to inconsistency between DD and DB

301 Redirect Using SAP Web Dispatcher