awk Command Generator — Build & Decode awk One-Liners
Extract columns, filter rows, sum or count — and get the exact awk one-liner
with every part explained. Or paste an awk command to decode it. Runs in
your browser.
Your command
—
Or: decode an awk command
Paste any awk one-liner — the program gets explained.
How awk thinks: fields and records
awk reads input one line (record) at a time and splits it into
fields you reference as $1, $2, … with $0
being the whole line. A program is pattern { action }: the action runs only on lines
that match the pattern. awk '{print $1}' prints the first column of every line;
awk '/ERROR/ {print $2}' prints the second column of lines containing ERROR.
The separator is the first thing to get right
By default awk splits on any run of whitespace, which is perfect for command output like
ls -l or ps. For CSV or /etc/passwd you must set the field
separator: -F',' or -F':'. Forget it and $2 won't be what you
expect.
Gotchas worth knowing
- Whitespace vs a single space. Default splitting collapses multiple spaces,
so columns line up even when the input is messy. Setting
-F' '(a literal single space) turns that off — usually not what you want. ENDruns once, after all lines. That's where totals live:awk '{s+=$1} END {print s}'adds column 1 across the file and prints the sum at the end.- Quote the whole program in single quotes. awk programs are full of
$and{}that the shell would otherwise mangle. - Conditions are just patterns.
awk '$3 > 100'with no action prints lines where column 3 exceeds 100 — the default action is "print the line".
Frequently asked questions
How do I print a specific column with awk?
awk '{print $2}' file prints the second column. For CSV, add the separator:
awk -F',' '{print $2}' file.
How do I sum a column with awk?
awk '{sum += $1} END {print sum}' file adds up column 1 and prints the total once at
the end.
How do I filter rows with awk?
Put a pattern before the action: awk '/ERROR/ {print}' for a text match, or
awk '$3 > 100' for a numeric condition.
How do I use a comma separator in awk?
awk -F',' '{print $1}'. The -F sets the field separator to a comma for
CSV files.