12.0 Professional Output: Pretty Printing with
12.1. Precision Formatting with
The printf function is AWK’s primary tool for producing highly formatted output. Borrowed from the C programming language, it provides granular control over spacing, alignment, number formatting, and character representation, going far beyond the capabilities of the simpler print statement.
12.2. Escape Sequences
Escape sequences are special character combinations used to represent non-printing or control characters within a string.
| Sequence | Description |
| \n | Newline |
| \t | Horizontal Tab |
| \v | Vertical Tab |
| \b | Backspace |
| \r | Carriage Return |
| \f | Form Feed |
- Newline (\n): Moves the cursor to the next line.
- Horizontal Tab (\t): Inserts a tab character for column alignment.
- Backspace (\b): Moves the cursor one position to the left, effectively erasing the preceding character if new text is printed over it.
- This example erases the last number from the first three fields. For instance, Field 1 is displayed as Field because the backspace removes the 1.
- Carriage Return (\r): Moves the cursor to the beginning of the current line without advancing. Any subsequent output will overwrite the existing text on that line.
12.3. Format Specifiers
Format specifiers are placeholders in the printf format string, beginning with %, that define how a corresponding argument should be formatted.
- %c: A single character.
- %d, %i: A decimal integer.
- %e, %E: A number in scientific notation (e.g., 1.23e+02).
- %f: A floating-point number.
- %g, %G: The shorter of %e or %f.
- %o: An unsigned octal number.
- %u: An unsigned decimal number.
- %s: A character string.
- %x, %X: An unsigned hexadecimal number (f vs. F).
- %%: A literal percent sign.
12.4. Format Modifiers (Optional Parameters)
Optional modifiers can be placed between the % and the specifier letter to further refine the output.
- Width: A number specifying the minimum field width. The output is padded with spaces (by default) to fill the width.
- – (Left Justification): A minus sign left-justifies the output within the specified field width.
- 0 (Leading Zeros): A leading zero pads numeric fields with zeros instead of spaces.
- + (Prefix Sign): Forces a sign (+ or -) to be displayed for all numbers.
- # (Alternate Form): Prepends 0 to octal numbers and 0x or 0X to hexadecimal numbers.
12.5. Concluding Summary
AWK’s enduring relevance stems from its masterful design. It combines a simple yet powerful Read, Execute, Repeat processing model with the expressive power of regular expressions and a rich set of features for data manipulation and formatted reporting. Its seamless integration into the command-line environment solidifies its status as an essential and indispensable tool for any serious shell scripter, system administrator, or data analyst working in a Unix-like environment.