< An Awk Primer

The output-redirection operator ">" can be used in Awk output statements. For example:

   print 3 > "tfile"

—creates a file named "tfile" containing the number "3". If "tfile" already exists, its contents are overwritten. The "append" redirection operator (">>") can be used in exactly the same way. For example:

   print 4 >> "tfile"

—tacks the number "4" to the end of "tfile". If "tfile" doesn't exist, it is created and the number "4" is appended to it.

Output redirection can be used with "printf" as well. For example:

   BEGIN {for (x=1; x<=50; ++x) {printf("%3d\n",x) >> "tfile"}}

—dumps the numbers from 1 to 50 into "tfile".

  • The output can also be "piped" into another utility with the "|" ("pipe") operator. As a trivial example, we could pipe output to the "tr" ("translate") utility to convert it to upper-case:
   print "This is a test!" | "tr [a-z] [A-Z]"

This yields:

   THIS IS A TEST!
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.