7.0 Managing Output: Redirection and Formatted Reporting
7.1 Advanced Formatting with
While the print statement is sufficient for simple, rapid output, the printf function is indispensable for generating professional, human-readable reports. Borrowed from the C language, it provides fine-grained control over alignment, padding, and data representation, enabling an engineer to transform raw data streams into polished, well-structured output suitable for direct consumption or further automated processing.
printf uses format specifiers and escape sequences to define the output structure.
Common Escape Sequences
| Sequence | Description |
| \n | Newline |
| \t | Horizontal Tab |
| \r | Carriage Return |
| \b | Backspace |
| \f | Form Feed |
| \v | Vertical Tab |
Common Format Specifiers
| Specifier | Description | Example |
| %s | A character string. | printf “Name = %s\n”, “Sherlock” |
| %d, %i | A signed decimal integer. | printf “Value = %d\n”, 80.66 (prints 80) |
| %f | A floating-point number. | printf “Value = %f\n”, 80.66 (prints 80.660000) |
| %e | A floating-point number in scientific notation (e.g., 1.23e+02). | printf “Value = %e\n”, 80.66 (prints 8.066000E+01) |
| %E | Same as %e, but uses E for the exponent. | printf “Value = %E\n”, 80.66 (prints 8.066000e+01) |
| %g, %G | Uses %f or %e format, whichever is shorter. | printf “Value = %g\n”, 80.66 (prints 80.66) |
| %c | A single character. | printf “Char: %c\n”, 65 (prints Char: A) |
| %o | An unsigned octal number. | printf “Octal: %o\n”, 10 (prints Octal: 12) |
| %x, %X | An unsigned hexadecimal number. | printf “Hex: %x\n”, 15 (prints Hex: f) |
| %% | A literal percent sign. | printf “Discount: 15%%\n” (prints Discount: 15%) |
printf also supports optional parameters between the % and the format specifier to control width and justification. For example, %-10s would left-justify a string in a 10-character field, while %05d would pad a decimal number with leading zeros to a width of 5.
7.2 Output Redirection, Appending, and Pipelining
AWK is designed to be a component in a larger toolchain, and it can seamlessly send its output to files or other processes. This allows AWK scripts to be integrated into larger shell scripting workflows for data persistence and post-processing.
There are three primary methods for handling output:
- Overwrite (>): Writes output to a specified file. If the file does not exist, it is created. If it already exists, its contents are overwritten.
- Syntax: print “data” > “output-file”
- Append (>>): Appends output to the end of a specified file. If the file does not exist, it is created.
- Syntax: print “data” >> “output-file”
- Pipe (|): Sends the output of the print or printf statement as the standard input to another command-line program.
- Syntax: print “data” | “command”
- Example: awk ‘BEGIN { print “hello, world” | “tr [a-z] [A-Z]” }’ sends the lowercase string to the tr command, which converts it to uppercase.
AWK also supports two-way communication with another process using the |& operator. This advanced technique allows an AWK script to send data to an external command and read the result back for further processing. The process can be demystified by breaking it down:
- print … |& cmd: The print statement sends data as standard input to the command stored in the cmd variable. The |& operator establishes the two-way pipe.
- close(cmd, “to”): After sending all data, this function call closes the “to” half of the pipe, signaling to the external command that there is no more input.
- cmd |& getline out: The external command processes its input and generates output. This expression reads one line of that output back into the AWK script and stores it in the out variable.
- close(cmd): Once all output has been read, the close function fully terminates the connection to the external process.