AWK Output
Every example so far has used print, which is simple but offers no control over spacing or formatting — fields just get separated by OFS and that's it. printf gives full control: column alignment, fixed decimal places, padded numbers — genuinely useful the moment output needs to look like a readable table rather than just raw values.
The Basic Syntax
A format string with placeholders (%s, %d, etc.), followed by a comma-separated list of values to fill those placeholders, in order. Unlike print, printf does not add a newline automatically — the \n has to be written explicitly, every time.
print spoiled this expectation by adding a newline for you automatically; printf never does.
Format Specifiers
| Specifier | Formats as |
|---|---|
| %s | String, as-is |
| %d | Integer (decimal) |
| %f | Floating-point number, default 6 decimal places |
| %.2f | Floating-point, exactly 2 decimal places — for currency, percentages |
| %5d | Integer, right-padded to a minimum width of 5 characters |
| %-10s | String, LEFT-aligned, padded to a minimum width of 10 characters |
| %% | A literal percent sign — needed because % alone starts a specifier |
Building an Aligned Table
This is where printf genuinely earns its place over plain print — turning ragged, inconsistently-spaced output into a clean, column-aligned table.
%-10s left-aligns the name within 10 characters; %5d right-aligns the integer within 5; %8.2f right-aligns the decimal within 8 characters, always showing exactly 2 decimal places — regardless of how differently-sized the original values were.
BEGIN block (Chapter 3) with a matching printf for column headers, using the same width specifiers, produces output that looks like a real formatted table rather than a raw data dump — a small addition that makes scripted reports far more usable.
BEGIN { printf "%-10s %5s %8s\n", "NAME", "AGE", "TOTAL" }
{ printf "%-10s %5d %8.2f\n", $1, $2, $3 }
' sales.txt
printf Inside sprintf — Building Strings Without Printing
sprintf behaves identically to printf but returns the formatted result as a string instead of printing it — useful when the formatted text needs to be used in further processing rather than output immediately.
Chapter 5 Quick Reference
- printf "format", values — no automatic newline, unlike print; \n must be explicit
- %s, %d, %f — string, integer, floating-point; %.2f — fixed decimal places
- %5d — right-aligned, min width 5; %-10s — left-aligned, min width 10
- %% — a literal percent sign
- Combine width specifiers across fields to build genuinely aligned table output
- sprintf — same formatting, returns a string instead of printing it
- Next chapter: arrays in AWK — associative arrays, counting, grouping