Go to the first, previous, next, last section, table of contents.


Redirecting Output to Files and Pipes

Here are the three forms of output redirection. They are all shown for the print statement, but they work identically for printf also.

print items > output-file
This type of redirection prints the items onto the output file output-file. The file name output-file can be any expression. Its value is changed to a string and then used as a file name (see section Expressions as Action Statements). When this type of redirection is used, the output-file is erased before the first output is written to it. Subsequent writes do not erase output-file, but append to it. If output-file does not exist, then it is created. For example, here is how one awk program can write a list of BBS names to a file `name-list' and a list of phone numbers to a file `phone-list'. Each output file contains one name or number per line.
awk '{ print $2 > "phone-list"
       print $1 > "name-list" }' BBS-list
print items >> output-file
This type of redirection prints the items onto the output file output-file. The difference between this and the single-`>' redirection is that the old contents (if any) of output-file are not erased. Instead, the awk output is appended to the file.
print items | command
It is also possible to send output through a pipe instead of into a file. This type of redirection opens a pipe to command and writes the values of items through this pipe, to another process created to execute command. The redirection argument command is actually an awk expression. Its value is converted to a string, whose contents give the shell command to be run. For example, this produces two files, one unsorted list of BBS names and one list sorted in reverse alphabetical order:
awk '{ print $1 > "names.unsorted"
       print $1 | "sort -r > names.sorted" }' BBS-list
Here the unsorted list is written with an ordinary redirection while the sorted list is written by piping through the sort utility. Here is an example that uses redirection to mail a message to a mailing list `bug-system'. This might be useful when trouble is encountered in an awk script run periodically for system maintenance.
report = "mail bug-system"
print "Awk script failed:", $0 | report
print "at record number", FNR, "of", FILENAME  | report
close(report)
We call the close function here because it's a good idea to close the pipe as soon as all the intended output has been sent to it. See section Closing Output Files and Pipes, for more information on this. This example also illustrates the use of a variable to represent a file or command: it is not necessary to always use a string constant. Using a variable is generally a good idea, since awk requires you to spell the string value identically every time.

Redirecting output using `>', `>>', or `|' asks the system to open a file or pipe only if the particular file or command you've specified has not already been written to by your program, or if it has been closed since it was last written to.


Go to the first, previous, next, last section, table of contents.