AWK scripting: 10 BEGIN and END block examples
BEGIN and END statements are help full in displaying information before and after executing actual AWK script. BEGIN block Uses: Declaring variables. Initialization variables for doing increment/decrements operations in main AWK code. Printing Headings/info before actual AWK code output. END block Uses: Printing final results, after doing operations in main AWK block. Printing Completion/info after actual AWK code output. AWK tool is mainly used for reporting some data in useful manner. With out these BEGIN and END blocks the output will be meaning less. In this post we will see different ways to use BEGIN and END blocks. For this post the example file is db.txt which contains below data: Jones 2143 78 84 77 Gondrol 2321 56 58 45 RinRao 2122234 38 37 Edwin 253734 87 97 95 Dayan 24155 30 47 What you get from above data? They are just names and followed by some numbers separated by spaces. If we can give some information at start of the file, such as what each column corresponding to etc as shown below Names total PPT Doc xls Jones 2143 78 84 77 Gondrol 2321 56 58 45 RinRao 2122234 38 37 0 Edwin 253734 87 97 95 Dayan 24155 30 47 0 This is more helpful to understand what actually this data is about. This type of output can be achieved by using BEGIN block. In this post we will see how to use BEGIN and END blocks with examples. AWK BEGIN block This...
Read More