Go forward to Close Output.
Go backward to Redirection.
Go up to Redirection.
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 Expressions as Action Statements: Expressions.).
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 Closing Output Files and Pipes: Close Output, 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.