The awk Command: Cheat Sheet and Examples
Most people’s entire relationship with awk is awk '{print $2}', typed from muscle memory and
understood only in the sense that it has always worked. This is not a criticism. It’s the
single most useful thing awk does, and plenty of careers have been built on less.
But awk is not a column-picker with an odd syntax. It’s a complete programming language,
written at Bell Labs in 1977 and named after its authors’ initials (Aho, Weinberger and
Kernighan, who presumably considered “WAK” and thought better of it). It has variables,
arrays, arithmetic and printf, it’s installed on every Unix-like machine you will ever SSH
into, and it does in one line the jobs people usually open a Python REPL for: sum a column,
count by category, reformat a report, deduplicate without sorting.
The useful part of the language fits on a page. This is that page.
Every ▶︎ Try it button on this page types its command into a real Linux machine running in this tab, with the tools already installed. Break it as much as you like.
The short version
Section titled “The short version”| You want | Command |
|---|---|
| One column | awk '{print $2}' file |
| One column of a command’s output | ps aux | awk '{print $2}' |
| The last column | awk '{print $NF}' file |
| Columns from a CSV | awk -F, '{print $1, $3}' file.csv |
| Only the lines that match | awk '$9 == 404' access.log |
| A column’s total | awk '{s += $10} END {print s}' access.log |
| A count for each value | awk '{n[$9]++} END {for (k in n) print k, n[k]}' access.log |
| Unique lines, original order | awk '!seen[$0]++' file |
| Disks more than 80% full | df -h | awk 'NR > 1 && $5+0 > 80' |
| A shell variable inside awk | awk -v code="$code" '$9 == code' access.log |
Everything in this guide sticks to POSIX awk, so it runs the same in macOS’s awk, GNU awk, mawk and BusyBox. Where they differ, we say so.
Files to practise on
Section titled “Files to practise on”Two small files carry the examples. The first is a web server log in the common log format,
the one nginx and Apache have been writing since before most of their users were born. Save it
as access.log:
192.0.2.10 - - [17/Nov/2026:09:14:02 +0000] "GET / HTTP/1.1" 200 5120192.0.2.10 - - [17/Nov/2026:09:14:03 +0000] "GET /css/site.css HTTP/1.1" 200 18432198.51.100.23 - - [17/Nov/2026:09:15:41 +0000] "GET /guides/ HTTP/1.1" 200 7340203.0.113.5 - - [17/Nov/2026:09:16:10 +0000] "GET /wp-login.php HTTP/1.1" 404 512203.0.113.5 - - [17/Nov/2026:09:16:11 +0000] "GET /.env HTTP/1.1" 404 512198.51.100.23 - - [17/Nov/2026:09:17:55 +0000] "GET /guides/awk/ HTTP/1.1" 200 24576192.0.2.44 - - [17/Nov/2026:09:20:30 +0000] "POST /api/search HTTP/1.1" 500 0192.0.2.44 - - [17/Nov/2026:09:20:31 +0000] "POST /api/search HTTP/1.1" 200 2210203.0.113.5 - - [17/Nov/2026:09:21:02 +0000] "GET /admin HTTP/1.1" 404 512198.51.100.23 - - [17/Nov/2026:09:24:18 +0000] "GET /img/keyboard.webp HTTP/1.1" 200 98304192.0.2.10 - - [17/Nov/2026:09:31:47 +0000] "GET /tools/ HTTP/1.1" 304 0192.0.2.44 - - [17/Nov/2026:09:33:05 +0000] "GET /guides/jq/ HTTP/1.1" 200 19840Yes, someone at 203.0.113.5 is looking for WordPress on a site that has never run it. Someone always is.
The second is a CSV of orders, saved as orders.csv:
order_id,date,customer,product,qty,price1001,2026-11-01,Acme Ltd,keyboard,2,129.001002,2026-11-01,Bloggs & Co,keycaps,5,24.501003,2026-11-02,Acme Ltd,cable,10,8.991004,2026-11-03,Initech,keyboard,1,129.001005,2026-11-03,Bloggs & Co,keyboard,3,129.001006,2026-11-04,Initech,keycaps,2,24.501007,2026-11-05,Acme Ltd,keycaps,4,24.50Or skip the saving: the Linux playground already has both in ~,
and the ▶︎ Try it buttons below type their commands into it. Its awk is BusyBox’s, which
gives the same output as every other awk for everything on this page.
How awk reads a file
Section titled “How awk reads a file”awk reads its input one line at a time (a record), splits each line into fields, and runs
your program against it. The program is a list of pattern { action } pairs: for every line
where the pattern is true, the action runs.
Either half is optional. No pattern means every line; no action means print the line. So
awk '{print $1}' is “for every line, print the first field”, and awk '$9 == 404' is “print
the lines whose ninth field is 404”. That is the whole model, and it explains most of the
one-liners you have ever copied. Give awk no file and it reads standard input instead, which
is how it ends up at the end of so many pipes; the examples below use the files so their
output is the same for everyone, and awk at the end of a pipe
turns the same tricks on other commands.
awk '{print $1, $9}' access.log | head -4192.0.2.10 200192.0.2.10 200198.51.100.23 200203.0.113.5 404Fields, and the variables that describe them
Section titled “Fields, and the variables that describe them”| Variable | Means |
|---|---|
$0 |
The whole line |
$1, $2 |
The first field, the second, and so on |
NF |
The number of fields on this line |
$NF |
The last field; $(NF-1) is the one before it |
NR |
The line number, counted across all the input |
FNR |
The line number within the current file |
FILENAME |
The name of the current file |
In the log, the bytes sent are the last field, and NR numbers the lines for free:
awk '{print NR": "$7, $NF}' access.log | head -31: / 51202: /css/site.css 184323: /guides/ 7340The comma in print matters. It puts a space (strictly, the output separator, more on which
shortly) between the values; leave it out and awk glues them together. print $1 $9 prints
192.0.2.10200, which is not an IP address of anything.
By default, fields are separated by any run of spaces and tabs, with leading and trailing
whitespace ignored. That’s why the log’s quoted request splits into three fields ("GET, the
path, and HTTP/1.1") and the status code lands in $9, not $7. It’s also what you want for
ps and df output, which pad their columns with as many spaces as it takes. To split on every
single space instead, empty fields and all, use -F'[ ]'.
Changing the separator
Section titled “Changing the separator”-F sets the field separator. For a CSV:
awk -F, 'NR > 1 {print $3}' orders.csvAcme LtdBloggs & CoAcme LtdInitechBloggs & CoInitechAcme LtdNR > 1 skips the header line, a pattern you’ll type a great deal. Note that “Acme Ltd”
survives in one piece: once the separator is a comma, spaces are just characters.
The separator can be a regular expression, which turns some fiddly jobs into one-liners. Split
the log on double quotes and the whole request is $2; split it on square brackets and the
timestamp is:
awk -F'"' '{print $2}' access.log | head -2awk -F'[][]' '{print $2}' access.log | head -2GET / HTTP/1.1GET /css/site.css HTTP/1.117/Nov/2026:09:14:02 +000017/Nov/2026:09:14:03 +0000([][] is a bracket expression containing ] and [, in the only order that parses. It looks
like a typo. It is not a typo.)
The output separator, and $1 = $1
Section titled “The output separator, and $1 = $1”OFS is what print puts between comma-separated values. Set it with -v, or in a BEGIN
block, which runs once before any input is read:
awk -F, -v OFS=' | ' '{print $1, $3}' orders.csv | head -3order_id | customer1001 | Acme Ltd1002 | Bloggs & CoNow try to convert the whole file, and nothing happens:
awk -F, -v OFS=' | ' '{print}' orders.csv | head -2order_id,date,customer,product,qty,price1001,2026-11-01,Acme Ltd,keyboard,2,129.00$0 is the line as it was read; awk only rebuilds it with OFS when a field changes. The
idiom for “change a field without changing it” is $1 = $1, which looks like a no-op and is
in fact the most important line in this section:
awk -F, -v OFS=' | ' '{$1 = $1; print}' orders.csv | head -2order_id | date | customer | product | qty | price1001 | 2026-11-01 | Acme Ltd | keyboard | 2 | 129.00The same trick, with OFS="\t", turns a CSV into tab-separated values. The flip side catches
people out: assign to any field and the line is rebuilt, so awk '{$2 = ""; print}' doesn’t
delete field two, it empties it and leaves two spaces where it was.
Patterns: choosing the lines
Section titled “Patterns: choosing the lines”A pattern can be a comparison, a regular expression, or both joined with &&, || and !.
awk '$9 == 404 {print $1, $7}' access.log203.0.113.5 /wp-login.php203.0.113.5 /.env203.0.113.5 /adminA bare /regex/ matches against the whole line; ~ matches against one field, and !~ is
its opposite:
awk '/POST/ {print $9, $7}' access.logawk '$7 ~ /^\/guides\// {print $7}' access.log500 /api/search200 /api/search/guides//guides/awk//guides/jq/Combine as many conditions as you like:
awk -F, '$4 == "keyboard" && $5 >= 2 {print $1, $3}' orders.csv1001 Acme Ltd1005 Bloggs & Co| Pattern | Matches |
|---|---|
NR > 1 |
Every line but the first (skip a header) |
NR == 3, NR == 5 |
Lines 3 to 5: a range, from the first match to the second |
/start/, /end/ |
From a line matching start to one matching end |
NF |
Lines that aren’t blank |
NR % 2 |
Odd-numbered lines |
length($0) > 80 |
Lines longer than 80 characters |
!seen[$0]++ |
The first appearance of each line |
That last one deserves its own section, and gets one further down.
Arithmetic, and the END block
Section titled “Arithmetic, and the END block”Variables need no declaring: one that has never been set is 0 in a sum and an empty string
everywhere else. That makes totals a matter of adding as you go and printing in END, which
runs once after the last line:
awk '{bytes += $10} END {print bytes}' access.logawk '{bytes += $10} END {print bytes / NR}' access.log17735814779.8Count lines that match, rather than adding a field:
awk '$9 == 404 {n++} END {print n + 0}' access.log3The + 0 is there for the day nothing matches. Without it n was never set, and awk prints
an empty line where you expected a zero, which is accurate but unhelpful in a script.
awk has + - * / % ^, int(), sqrt(), exp(), log(), sin(), cos(), atan2() and
rand(), and all its numbers are floating point, so 7 / 2 is 3.5 without any persuasion.
Arrays: counting and grouping
Section titled “Arrays: counting and grouping”This is where awk stops being a column-picker. Arrays are associative (keyed by any string), spring into existence when you use them, and turn “count by X” or “total by X” into one line. Count requests by status code:
awk '{count[$9]++} END {for (s in count) print s, count[s]}' access.log | sort200 7304 1404 3500 1Total bytes by client, biggest first:
awk '{ bytes[$1] += $10 } END { for (ip in bytes) print bytes[ip], ip }' access.log | sort -rn130220 198.51.100.2323552 192.0.2.1022050 192.0.2.441536 203.0.113.5It’s GROUP BY for people with no database, and on a log file of a few million lines it
finishes in about the time it takes to remember the SELECT syntax.
key in array tests for a key without creating it (a plain array[key] reference creates
it), and delete array[key] removes one.
Deduplicating without sorting
Section titled “Deduplicating without sorting”sort -u removes duplicate lines, and rearranges the rest in the process. awk can keep the
original order:
awk '!seen[$1]++ {print $1}' access.log192.0.2.10198.51.100.23203.0.113.5192.0.2.44Read it inside out. seen[$1]++ returns the count before adding one, so it’s 0 the first
time a value appears and something larger every time after. ! turns that into true for the
first appearance only, and a pattern with no action prints. Drop the {print $1} and
awk '!seen[$0]++' deduplicates whole lines, which is the version that ends up in everyone’s
shell aliases.
if, else and next
Section titled “if, else and next”Patterns choose which lines an action runs on; if makes the choice inside the action, which
is the tidier option once there are more than two outcomes. The syntax is C’s, braces
optional for a single statement, and a newline or ; between statements. A program inside
single quotes can run over as many lines as it likes; the shell waits for the closing quote.
awk '{ if ($9 >= 500) s = "server error" else if ($9 >= 400) s = "client error" else s = "ok" print $9, s, $7 }' access.log | head -7200 ok /200 ok /css/site.css200 ok /guides/404 client error /wp-login.php404 client error /.env200 ok /guides/awk/500 server error /api/searchFor a choice between two values, the ternary operator fits inside a print. Wrap it in
brackets, or awk may read the > in it as a redirect:
awk -F, 'NR > 1 {print $1, $3, ($5 >= 3 ? "bulk" : "single")}' orders.csv1001 Acme Ltd single1002 Bloggs & Co bulk1003 Acme Ltd bulk1004 Initech single1005 Bloggs & Co bulk1006 Initech single1007 Acme Ltd bulknext abandons the current line and starts on the next one, skipping every rule below it.
It’s how a program with several rules says “this line is dealt with”: here the header is
skipped, keyboards are counted, and only what’s left falls through to the last rule.
awk -F, 'NR == 1 { next } $4 == "keyboard" { kb += $5; next } { other += $5 } END { print kb, "keyboards,", other, "other items" }' orders.csv6 keyboards, 21 other itemsprintf, for output that lines up
Section titled “printf, for output that lines up”print is fine until the columns start wobbling. printf takes a C-style format: %s for a
string, %d for a whole number, %.2f for two decimal places, a number for the width, and
- to left-align. Unlike print it adds no newline, so end the format with \n.
awk -F, 'NR > 1 { line = $5 * $6; sum += line printf "%-12s %-9s %8.2f\n", $3, $4, line } END { printf "%-22s %8.2f\n", "Total", sum }' orders.csvAcme Ltd keyboard 258.00Bloggs & Co keycaps 122.50Acme Ltd cable 89.90Initech keyboard 129.00Bloggs & Co keyboard 387.00Initech keycaps 49.00Acme Ltd keycaps 98.00Total 1133.40That is an invoice summary from a CSV in one command, which a spreadsheet would want you to open, import, confirm the delimiter for and then save in a format of its choosing. Group it by customer and it becomes a sales report:
awk -F, 'NR > 1 { spend[$3] += $5 * $6 } END { for (c in spend) printf "%8.2f %s\n", spend[c], c }' orders.csv | sort -rn 509.50 Bloggs & Co 445.90 Acme Ltd 178.00 Initechprintf is also the cure for awk’s habit of printing numbers with up to six significant
digits: printf "%.1f KiB\n", $10 / 1024 gives 7.2 KiB where print gives 7.16797.
Passing in shell variables
Section titled “Passing in shell variables”Sooner or later the 404 becomes a variable. The tempting fix is to swap the single quotes for double ones so the shell can fill it in:
awk "\$9 == $code" access.log # don'tThis works right up until $code is empty, or contains a quote, or comes from somewhere you
don’t control, and then you’re debugging a program assembled by string concatenation. Pass
values in with -v instead, and they arrive as awk variables:
code=404awk -v code="$code" '$9 == code {print $7}' access.log/wp-login.php/.env/adminFor environment variables, there’s ENVIRON["HOME"].
String functions
Section titled “String functions”| Function | Does |
|---|---|
length(s) |
Length of s; with no argument, of the whole line |
substr(s, start, len) |
Part of s; counting starts at 1, not 0 |
index(s, t) |
Position of t in s, or 0 |
split(s, arr, sep) |
Splits s into arr[1], arr[2]… and returns the count |
sub(re, repl, s) |
Replaces the first match of re in s (default $0) |
gsub(re, repl, s) |
Replaces every match; returns how many |
match(s, re) |
Position of re in s, and sets RSTART and RLENGTH |
tolower(s), toupper(s) |
What they say |
sprintf(fmt, …) |
printf into a string instead of the output |
sub and gsub change their target in place, which makes them unlike the string functions of
every language you’ve used since, and is easy to forget. Strip the quote off the request
method and count them:
awk '{gsub(/"/, "", $6); print $6}' access.log | sort | uniq -c 10 GET 2 POSTsplit pulls a timestamp apart; here the hour and minute of the first three requests:
awk '{split($4, t, ":"); print t[2]":"t[3]}' access.log | head -309:1409:1409:15Writing to several files
Section titled “Writing to several files”print can redirect, inside awk, to a file named by any expression. Split the log into one
file per status code:
awk '{print > ($9 ".log")}' access.logcat 500.log192.0.2.44 - - [17/Nov/2026:09:20:30 +0000] "POST /api/search HTTP/1.1" 500 0The brackets round the file name matter: without them, awks disagree about which part of
print > $9 ".log" is the file name and which is being printed. Unlike the shell, > empties
each file only the first time it’s opened, then keeps appending for the rest of the run. Use
>> to keep what was there before.
Editing a file in place
Section titled “Editing a file in place”awk never changes its input file. The obvious attempt, awk '...' orders.csv > orders.csv,
loses the lot: the shell empties the file before awk gets to read it. Write to a temporary file
and move it over the original instead. Here, a 10% price rise:
awk -F, -v OFS=, 'NR > 1 {$6 = sprintf("%.2f", $6 * 1.1)} 1' \ orders.csv > orders.tmp && mv orders.tmp orders.csvThe lone 1 is a pattern that’s always true with no action, so it prints every line, changed
or not. GNU awk’s -i inplace does the temporary file for you; no other awk has it.
awk at the end of a pipe
Section titled “awk at the end of a pipe”Everything above works on any command’s output, because to awk a pipe is just a file that arrives a line at a time. Most command output is the shape awk likes best: a header, then one row per thing, in columns separated by spaces. The recipes below are the ones people actually search for.
df: disks that are filling up
Section titled “df: disks that are filling up”df prints a header, then one line per filesystem with the percentage used in the fifth column:
df -h | awk 'NR > 1 && $5+0 > 80'backups 64.0K 56.0K 8.0K 88% /srv/backupsThe +0 is the important part. 88% doesn’t look like a number, so $5 > 80 compares it as
text, and as text 9% is greater than 80 as well. Adding zero takes the number at the front
and drops the %.
ps: processes eating the CPU
Section titled “ps: processes eating the CPU”ps aux puts the PID in column 2, %CPU in column 3 and the command in column 11, on Linux and
macOS alike:
ps aux | awk 'NR > 1 && $3 > 50 {print $2, $11}'Two catches. Column numbers belong to the program that printed them, not to awk, and not every
ps agrees: BusyBox’s, on Alpine and in most containers (the playground included), prints
PID USER TIME COMMAND and nothing else, so $2 there is the user. Check the header before
you trust a column number from the internet. And ps aux | awk '/nginx/' finds the awk
process too, because its own command line contains “nginx”. Match a field instead
($11 ~ /nginx/), or use pgrep, which exists for exactly this.
history: your most-used commands
Section titled “history: your most-used commands”history numbers each line, so the command’s name is $2:
history | awk '{n[$2]++} END {for (c in n) print n[c], c}' | sort -rn | headZsh’s history lists only the last sixteen commands; history 1 lists the lot. The output is
yours, and statistically it’s git, cd and ls, in some order, followed by vim, which
you have opened rather more often than you’ve managed to close.
git diff: lines added and removed
Section titled “git diff: lines added and removed”git diff --numstat prints added and removed counts for each file, and two running totals turn
that into one summary:
git diff --numstat | awk '{a += $1; d += $2} END {print "+" a, "-" d}'+4 -1Binary files show - for both counts, which awk adds as zero. That’s convenient here and
worth knowing elsewhere: text that isn’t a number is quietly worth nothing in a sum.
$PATH: one entry per line
Section titled “$PATH: one entry per line”This one takes a different separator, and a loop over the fields:
echo "$PATH" | awk -F: '{for (i = 1; i <= NF; i++) print $i}'/usr/local/sbin/usr/local/bin/usr/sbin/usr/bin/sbin/binThe cleverer-looking awk -v RS=: '1' makes each entry a record instead, and prints a blank
line at the end for its trouble, because the last record keeps echo’s newline. tr : '\n'
is shorter than both, and nobody will think less of you.
kubectl: pods that aren’t running
Section titled “kubectl: pods that aren’t running”kubectl get pods prints NAME READY STATUS RESTARTS AGE, so:
kubectl get pods | awk 'NR > 1 && $3 != "Running" {print $1}'The same shape works for docker ps and most other commands that print a table, as long as you
stay left of the first column that can contain spaces: docker ps’s STATUS, Up 2 hours, is
three fields on its own, and everything after it moves. Many of them also have filters and
output formats of their own (--field-selector, -o jsonpath, --format), which don’t break
when a column is added or a value contains a space. Those are the better choice in a script
you’ll keep; awk is the one that works on all of them, and the one you can type before you’ve
read anyone’s documentation.
awk gotchas
Section titled “awk gotchas”Numbers that compare as text
Section titled “Numbers that compare as text”awk decides whether a value is a number by looking at it. A field that looks like a number
compares as one, so 10 < 9 is false, as it should be. But "web-10" < "web-9" compares as
text and is true, and a value read from a file with a stray space or a currency sign stops
looking like a number at all. Force the issue with $1 + 0 for a number or $1 "" for a
string.
CSV fields with commas in quotes
Section titled “CSV fields with commas in quotes”-F, splits on every comma, including those inside quoted fields:
echo '1007,2026-11-05,"Smith, Jones & Partners",cable,4,8.99' | awk -F, '{print $3}'"SmithGNU awk 5.3 and later, and Kernighan’s own awk since 2023, understand CSV quoting with
--csv; macOS’s awk, mawk and BusyBox don’t. For anything you didn’t generate yourself, a
real CSV tool (or gawk --csv) saves the afternoon.
gawk, mawk or BusyBox: which awk have you got?
Section titled “gawk, mawk or BusyBox: which awk have you got?”There are four common awks, and which one you’re running depends on where you are:
| awk | Where you’ll find it | Extras worth knowing |
|---|---|---|
| The one true awk (“BWK awk”) | macOS, the BSDs | Kernighan’s own; --csv since 2023 |
GNU awk (gawk) |
Fedora, Arch, most non-Debian Linux | --csv, asort, strftime, -i inplace, a debugger |
| mawk | Debian and Ubuntu, by default | Speed, mainly |
| BusyBox awk | Alpine, containers, routers | Being there at all |
awk --version names the first three. BusyBox doesn’t recognise the option, and answers with
its usage message instead, which starts with the word BusyBox and so does the job anyway.
awk, sed, cut, or something else?
Section titled “awk, sed, cut, or something else?”They overlap, and the choice is mostly about which one makes the job a single obvious line:
cutfor a fixed column on a single-character delimiter, and nothing else. It can’t handle runs of spaces, which rules out most command output, butcut -d, -f3is hard to beat for a simple CSV.sedfor substitutions on whole lines: change this string to that, delete lines matching this. As soon as you’re thinking in columns, it’s the wrong tool.- awk for anything involving fields, arithmetic or remembering something between lines.
- ripgrep to find the lines first; on a large tree,
rg pattern | awk ...beats asking awk to search. - jq the moment the input is JSON. awk can be bullied into pulling fields out of JSON, but it’s line-oriented and JSON isn’t, and the result breaks the first time someone reformats the file.
- Python when the one-liner has acquired a second screen, a function or three, and a comment explaining the comment. awk will run a 200-line program, but your colleagues would rather you didn’t.
Where next
Section titled “Where next”awk is at its best at the end of a pipe, so it turns up throughout the fzf
guide, picking process IDs and file names out of whatever you
selected. If awk is about to go into a script, read Bash strict
mode first: piping a long awk into
head is the classic way to meet pipefail’s one annoying habit. The modern CLI starter
pack covers the tools that find the files to feed it, and the
minimal Zsh setup is where the one-liners you
keep retyping should end up as aliases.