|
|
You can print output to files instead of to the standard output by using the > and >> redirection operators. For example, the following program invoked on the file countries prints all lines where the population (third field) is bigger than 100 into a file called bigpop, and all other lines into a file called smallpop:
$3 > 100 { print $1, $3 >"bigpop" }
$3 <= 100 { print $1, $3 >"smallpop" }
Notice that the filenames have to be quoted;
without quotes,
bigpop
and
smallpop
are merely uninitialized variables.
If the output filenames were created by an expression,
they would also have to be enclosed in parentheses:
$4 ~ /North America/ { print $1 > ("tmp" FILENAME) }
because the > operator has higher precedence than concatenation;
without parentheses, the
concatenation of tmp and FILENAME
would not work.