Structure of Awk Programs
Structure of AWK Program :
An awk program consists of following structure :
BEGIN
pattern { action }
pattern { action }
pattern { action }
pattern { action }
END
Explanation :
Part |
Optional |
Explanation |
BEGIN |
Yes |
Used for data processing before the input data is read or processed |
Pattern Action Pair |
No |
Process the Input data |
END |
Yes |
Used for data processing after the input data is read or processed |
Comments |
Yes |
Denoted by '#' and used for documentation purpose. |
Pattern and Action Pair :
- The Pattern specifies when the action is performed.
- Each programming statement in AWK contains a pattern or action or both.
- The patterns are listed without any parentheses or braces while the actions are enclosed in braces ({}).
- Each line read will be considered as single input as AWK is line oriented.
Default Pattern and Action
Default Pattern |
Match all the lines in the input data |
Default Action |
Print the current record of the input data |
How AWK actually works ?
- AWK will scan the input data sequentially line by line
- AWK will searches single line input for predefined pattern
- AWK then manipulates input according to the action specified when the pattern is encountered.
- Pattern may be - AWK Expression,Regular expression or Boolean Expression.
Example :
BEGIN { print "START" }
{ print }
END { print "STOP" }