This example program outputs the words "hello world" to the terminal:
# Hello World BEGIN { print "Hello World!" exit }
The program consists of the following elements:
In awk, comments are inserted by prefixing them with a [[crosshatch?]] symbol. Any comments are ignored by the awk interpreter, which ignores the hash symbol and any other characters that follow it until the end of the line:
# This whole line is a comment and is ignored by the awk interpreter print 'Hello' # Comments after a hash sign are ignored
The BEGIN block is executed once only at the start of the program, before any records have been read, providing startup actions within the program.
The contents of the begin function is enclosed in braces. This defines the boundaries of the [[block?]] of code that the [[function?]] contains.
The first statement within the main function calls the print statement with a string of [[character?]]s, which are output to the screen.