Invoking an awk program

Throwaway one-liners

It is often useful to type a simple program at the command line for use within awk. An awk program can be supplied as an [[argument?]] of the awk command:

 awk 'BEGIN {print "Hello World!"}'

Running an awk script

An awk script can be started at the command line, by passing the script name as a parameter to the awk interpreter:

 awk -f foobar.awk

Using a shebang (hashbang)

As with other Unix scripts, an awk script can be started using a hashbang. However, because of the syntax of the awk command line, it is necessary to include a the -f switch in the hashbang line as follows:

#!/usr/bin/awk -f
print "Hello World!"

Using awk as a filter