Table of Content

AWK Program Structure

Structure of AWK Program :

An awk program consists of following structure :

    pattern { action }
    pattern { action } 
    pattern { action } 
    pattern { action }

Explanation :

BEGINYesUsed for data processing before the input data is read or processed
Pattern Action PairNoProcess the Input data
ENDYesUsed for data processing after the input data is read or processed
CommentsYesDenoted by '#' and used for documentation purpose.

Pattern and Action Pair :

  1. The Pattern specifies when the action is performed.
  2. Each programming statement in AWK contains a pattern or action or both.
  3. The patterns are listed without any parentheses or braces while the actions are enclosed in braces ({}).
  4. Each line read will be considered as single input as AWK is line oriented.

Default Pattern and Action

Default PatternMatch all the lines in the input data
Default ActionPrint the current record of the input data

How AWK actually works ?

  1. AWK will scan the input data sequentially line by line
  2. AWK will searches single line input for predefined pattern
  3. AWK then manipulates input according to the action specified when the pattern is encountered.
  4. Pattern may be – AWK Expression,Regular expression or Boolean Expression.

Example :

BEGIN { print "START" }
      { print         }
END   { print "STOP"  }