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 is a block code which is executed before executing actual AWK script
Syntax for BEGIN block is
awk 'BEGIN{awk initializing code}{actual AWK code}' filename.txt
Example1: Print a meaning full info before actual AWK output.
awk 'BEGIN{print "########################nThis is the output of filtered datan########################"}{print $0}' db.txt
Output:
########################## This is the output of filtered data ########################## Jones 2143 78 84 77 Gondrol 2321 56 58 45 RinRao 2122234 38 37 Edwin 253734 87 97 95 Dayan 24155 30 47
Example2: How about giving each column names along with data? We can name each column as Name, pass code,
awk 'BEGIN{print "NamesttotaltPPTtDoctxls"}{printf "%-st%dt%dt%dt%dn", $1,$2,$3,$4,$5}' db.txt
Output:
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
Example3: How about changing the output field separator to "|" we can do that by using OFS inbuilt variable in BEGIN block.
awk 'BEGIN{OFS="|"}{print $1,$2,$3,$4,$5}' db.txt
Output:
Jones|2143|78|84|77 Gondrol|2321|56|58|45 RinRao|2122234|38|37| Edwin|253734|87|97|95 Dayan|24155|30|47|
Example4: How about converting /etc/passwd to above format? We can do that by defining input field separator "FS" to ":".
awk 'BEGIN{FS=":";OFS="|"; }{print $1,$2,$3,$4,$5,$6,$7}' /etc/passwd | head -5
Output:
root|x|0|0|root|/root|/bin/bash daemon|x|1|1|daemon|/usr/sbin|/bin/sh bin|x|2|2|bin|/bin|/bin/sh sys|x|3|3|sys|/dev|/bin/sh sync|x|4|65534|sync|/bin|/bin/sync
Note1: Make note that by default input field separator is either space or tab.
Note2: We can use either FS built-in variable or -F option to specify input field separator.
Note2: We can use either FS built-in variable or -F option to specify input field separator.
awk -F":" 'BEGIN{OFS="|"; }{print $1,$2,$3,$4,$5,$6,$7}' /etc/passwd | head -5
Output:
root|x|0|0|root|/root|/bin/bash daemon|x|1|1|daemon|/usr/sbin|/bin/sh bin|x|2|2|bin|/bin|/bin/sh sys|x|3|3|sys|/dev|/bin/sh sync|x|4|65534|sync|/bin|/bin/sync
Example5: We can use even BEGIN block to initialize user defined variables in it. I want to add my 3rd column for every iteration to the same value.
My 3rd column
78 56 21 87 55
I want to add these numbers and display every addition when it happen
awk 'BEGIN{s=0}{s=s+$3;print s}' db.txt
Output:
78 134 155 242 297
We initialized s in BEGIN block and incremented column 3 values in actual AWK code block.
AWK END BLOCK
This is piece of block which is executed after executing all the AWK code. This is the last AWK code going to execute.
Example6: Print some meaning full info after processing AWK code.
awk 'END{print "#########################/nCompleted printing filtered data/n########################"}{print $0}' db.txt
Output:
Jones 21 78 84 77 Gondrol 23 56 58 45 RinRao 25 21 38 37 Edwin 25 87 97 95 Dayan 24 55 30 47 ######################### Completed printing filtered data #########################
COMBINING BEGIN AND END BLOCKS
Example7: Print some meaning full before and after AWK code execution.
awk 'BEGIN{print "##########################/nThis is the output of filtered data/n##########################"}{print $0}END{print "########################/nCompleted printing filterd data/n########################"}' db.txt
Output:
######################### This is the output of filtered data ######################### Jones 21 78 84 77 Gondrol 23 56 58 45 RinRao 25 21 38 37 Edwin 25 87 97 95 Dayan 24 55 30 47 ######################## Completed printing filtered data ########################
Example8: How about printing headings for each column?
awk 'BEGIN{print "##########################nThis is the output of filtered datan###########################";print "NamesttotaltPPTtDoctxls"}{printf "%st%dt%dt%dt%dn", $1,$2,$3,$4,$5}END{print "###########################nCompleted printing filtered datan###########################"}' db.txt
Output:
########################### This is the output of filtered data ########################### Names total PPT Doc xls Jones 21 78 84 77 Gondrol 23 56 58 45 RinRao 25 21 38 37 Edwin 25 87 97 95 Dayan 24 55 30 47 ########################### Completed printing filtered data ###########################
Example9: How to adding all the numbers in 4th column and print the total value at the end.
awk 'BEGIN{s=0}{s=s+$3}END{print s}' db.txt
Output:
297
There is a slight difference between Example 5 and this. We used END block to print the last sum instead of printing the sum for every iteration. In our next post we will see how to use AWK inbuilt variables in detail.
0 comments:
Post a Comment