END

The awk programming language allows us to use end blocks to provide [[finishoff?]] and [[cleanup?]] actions within the program.

Labelling

An end block is marked with the END label:

 END {
   # This is a begin block
   print "The end is here" # Print the closing message
 }

Execution

End blocks can are executed once only, after the last input record is read.

End blocks must contain actions

There is no default action for end blocks, so end block must contain defined actions. An end block must contain actions.

There is no current record information available within an end block

There is no current record information available within an end block, because end blocks are executed after the last input record has been read.

Programming Convention

The original version of awk required the *end block* to be placed at the end of the program and only one end block was allowed. This is no longer a requirement of current awk versions, however it is recommended that this convention is observed for portability and readability purposes.

Multiple End Blocks

Multiple end blocks are executed in order in which they are defined.