Hello World in awk

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:

Comment Block

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

Begin Blocks

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.

Squiggly Brackets

The contents of the begin function is enclosed in braces. This defines the boundaries of the [[block?]] of code that the [[function?]] contains.

The print statement

The first statement within the main function calls the print statement with a string of [[character?]]s, which are output to the screen.