Redirecting output to file
Till now we have been sending the output of print
and printf
commands to stdout
, that is, the screen. However, we can also redirect the output to files by using the redirection operator.Redirection is done after the print
command. It is the same as we do in shell commands using redirection operator.
There are three forms of output redirection:
- Output to file
- Output appended to a file
- Output through a pipe to another command
Redirecting output to a file (>)
This redirection operator (>
) prints the items into the output file. Its syntax is as follows:
print items > demo
In this type of redirection, if the output file named demo
exists, then it is erased before the first output is written to it. Subsequent write operations to the same file within the same AWK command do not overwrite the content, but append to it. If the output file does not it creates it. For example, with the employee database file, emp.dat
, we generate a report with headers columns, as follows...