Go to the first, previous, next, last section, table of contents.
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
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
awk output is
appended to the file.
print items | command
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.