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 printe...