上QQ阅读APP看书,第一时间看更新
The END block
The END block is executed after the last input line of the last input file is read. This block is executed at the end and is generally used for rendering totals, averages, and for processing data and figures that were read in the various input records. The syntax of the END block is as follows:
END { action / awk-commands }
We can have multiple BEGIN or END blocks in a program. The action in that block will get executed as per the appearance of the block in that program. It is not mandatory to have BEGIN first and END last. The BEGIN and END blocks do not contain patterns, they contain action statements only.
Here is an example of the usage of the BEGIN and END blocks:
$ awk 'BEGIN { print "==Employee Info==" } # begin block
{ print } # body block
END { print "==ends here==" }' empinfo.txt # end block
On executing the code, we get the following result:
==Employee Info==
Jack 9857532312 jack@gmail.com hr 2000 5
Jane 9837432312 jane@gmail.com hr 1800 5
Eva 8827232115 eva@gmail.com lgs 2100 6
amit 9911887766 amit@yahoo.com lgs 2350 6
Julie 8826234556 julie@yahoo.com hr 2500 5
===ends here==