redirection

The awk scripting language provides support for redirection via the greaterthan, doublebeak and pipe symbols. The greaterthan symbol acts as an output redirection operator allowing output from a function to be written to a named file. If the named file exists, its contents will be overwritten by the output from the function:

 BEGIN {
   # Creates a file foobar.txt
   # If the file already exists, its contents will be overwritten
   print "Hello" > foobar.txt
 }

A doublebeak symbol behaves in a similar manner to the greaterthan symbol, but instead of the file being overwritten, the output from the function will be appended to the end of the file:

 BEGIN {
   # Append a message to the end of the file foobar.txt
   print "Hello" >> foobar.txt
 }

Redirection to standard error

The traditional way to redirect output to standard error is by using a kludge to pipeline redirection to an external cat command:

 print "TST60000 An error has occured!" | "cat 1>&2"

On systems that provide /dev/stderr, redirection can be made to this device:

 print "TST60000 An error has occured!" > /dev/stderr