Skip to content
Latchkey

awk printf: Format Numbers and Columns

awk printf writes formatted output using C-style specifiers, giving you control over width, padding, and decimal places that print lacks.

When a report needs aligned columns or a fixed number of decimals, printf is the tool. Unlike print, it adds no newline, so you include \n yourself.

What it does

printf takes a format string with %-specifiers and a list of arguments, substituting each in turn. It does not append a newline (you write \n) and does not insert OFS between arguments. Specifiers control type, width, and precision.

Common usage

Terminal
# two decimal places
awk '{printf "%.2f\n", $1}' nums.txt
# left-aligned name, right-aligned count
awk '{printf "%-20s %5d\n", $1, $2}' tally.txt
# percentage from a ratio
awk '{printf "%.1f%%\n", $1/$2*100}' ratios.txt
# hex and integer
awk 'BEGIN{printf "%d %x\n", 255, 255}'

Format specifiers

SpecifierWhat it formats
%sString
%dInteger (truncates a float)
%.2fFloat with two decimal places
%-10sString left-aligned in a 10-wide field
%5dInteger right-aligned in a 5-wide field
%%A literal percent sign

In CI

printf "%.2f" is how you keep a coverage or latency number to a stable precision before writing it to $GITHUB_OUTPUT, so downstream comparisons are consistent. Remember to add \n; without it values run together on one line.

Common errors in CI

Forgetting \n in printf concatenates every record onto one line. A format with more % specifiers than arguments prints 0 or empty for the missing ones; too few specifiers drops arguments. "%d" on a value like "12ms" prints 12 (the numeric prefix). A bare % that is not %% can produce unexpected output or, in some awks, an error.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →