DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Using awk

Output into files

You can print output into files, instead of to the standard output, using the ``>'' and ``>>'' redirection operators. For example, if you invoke the following program on the file countries, awk prints all lines where the population (third field) is bigger than 100 into a file called bigpop, and all other lines into smallpop:

   $3 > 100   { print $1, $3 >"bigpop" }
   $3 <= 100  { print $1, $3 >"smallpop" }
Notice that the filenames must be quoted; without quotes, bigpop and smallpop are uninitialized variables. If the output filenames are created by an expression, they also must be enclosed in parentheses:
   $4 ~ /North America/ { print $1 > ("tmp" FILENAME) }
This is because the > operator has higher precedence than concatenation; without parentheses, the concatenation of tmp and FILENAME does not work.

Note that files are opened once in an awk program. If > is used to open a file, its original contents are overwritten. But if >> is used to open a file, its contents are preserved and the output is appended to the file. Once the file has been opened, the two operators have the same effect.


Next topic: Output into pipes
Previous topic: awk printf conversion characters

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003