skip to main content

SAP NetWeaver Newbie

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

2 comments:

  1. Using END block only:

    I forgot to add that you need to be using atleast BEGIN, main or END block with awk. The post shows you examples with BEGIN only and main only block usage. Here is END only block.

    Let's consider a file (/mytestinput) with the following lines:

    sap employment
    in memory database
    awk examples
    xi jobs
    password protection software
    awk print columns
    sap vacancies

    Here is how I can use END only block:
    awk 'END{print $0}' /mytestinput
    sap vacancies

    The input to awk is passed one line at a time. when the last line is hit, the END block is called. The end block has print $) statement. Meaning print the input line, and the last line in the file is printed. This example shows how awk can be used instead of tail -1.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

Email Subscription

Get every new post into your inbox by subscribing us.

Want a reason to subscribe?
1. This sitemap might convince you to subscribe.
2. We do not misuse email IDs. We respect privacy.

© 2008 - 2017 sapnwnewbie. All rights reserved.