Skip to content

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.

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.

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 5120
192.0.2.10 - - [17/Nov/2026:09:14:03 +0000] "GET /css/site.css HTTP/1.1" 200 18432
198.51.100.23 - - [17/Nov/2026:09:15:41 +0000] "GET /guides/ HTTP/1.1" 200 7340
203.0.113.5 - - [17/Nov/2026:09:16:10 +0000] "GET /wp-login.php HTTP/1.1" 404 512
203.0.113.5 - - [17/Nov/2026:09:16:11 +0000] "GET /.env HTTP/1.1" 404 512
198.51.100.23 - - [17/Nov/2026:09:17:55 +0000] "GET /guides/awk/ HTTP/1.1" 200 24576
192.0.2.44 - - [17/Nov/2026:09:20:30 +0000] "POST /api/search HTTP/1.1" 500 0
192.0.2.44 - - [17/Nov/2026:09:20:31 +0000] "POST /api/search HTTP/1.1" 200 2210
203.0.113.5 - - [17/Nov/2026:09:21:02 +0000] "GET /admin HTTP/1.1" 404 512
198.51.100.23 - - [17/Nov/2026:09:24:18 +0000] "GET /img/keyboard.webp HTTP/1.1" 200 98304
192.0.2.10 - - [17/Nov/2026:09:31:47 +0000] "GET /tools/ HTTP/1.1" 304 0
192.0.2.44 - - [17/Nov/2026:09:33:05 +0000] "GET /guides/jq/ HTTP/1.1" 200 19840

Yes, 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,price
1001,2026-11-01,Acme Ltd,keyboard,2,129.00
1002,2026-11-01,Bloggs & Co,keycaps,5,24.50
1003,2026-11-02,Acme Ltd,cable,10,8.99
1004,2026-11-03,Initech,keyboard,1,129.00
1005,2026-11-03,Bloggs & Co,keyboard,3,129.00
1006,2026-11-04,Initech,keycaps,2,24.50
1007,2026-11-05,Acme Ltd,keycaps,4,24.50

Or 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.

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.

Terminal window
awk '{print $1, $9}' access.log | head -4
192.0.2.10 200
192.0.2.10 200
198.51.100.23 200
203.0.113.5 404

Fields, 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:

Terminal window
awk '{print NR": "$7, $NF}' access.log | head -3
1: / 5120
2: /css/site.css 18432
3: /guides/ 7340

The 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'[ ]'.

-F sets the field separator. For a CSV:

Terminal window
awk -F, 'NR > 1 {print $3}' orders.csv
Acme Ltd
Bloggs & Co
Acme Ltd
Initech
Bloggs & Co
Initech
Acme Ltd

NR > 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:

Terminal window
awk -F'"' '{print $2}' access.log | head -2
awk -F'[][]' '{print $2}' access.log | head -2
GET / HTTP/1.1
GET /css/site.css HTTP/1.1
17/Nov/2026:09:14:02 +0000
17/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.)

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:

Terminal window
awk -F, -v OFS=' | ' '{print $1, $3}' orders.csv | head -3
order_id | customer
1001 | Acme Ltd
1002 | Bloggs & Co

Now try to convert the whole file, and nothing happens:

Terminal window
awk -F, -v OFS=' | ' '{print}' orders.csv | head -2
order_id,date,customer,product,qty,price
1001,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:

Terminal window
awk -F, -v OFS=' | ' '{$1 = $1; print}' orders.csv | head -2
order_id | date | customer | product | qty | price
1001 | 2026-11-01 | Acme Ltd | keyboard | 2 | 129.00

The 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.

A pattern can be a comparison, a regular expression, or both joined with &&, || and !.

Terminal window
awk '$9 == 404 {print $1, $7}' access.log
203.0.113.5 /wp-login.php
203.0.113.5 /.env
203.0.113.5 /admin

A bare /regex/ matches against the whole line; ~ matches against one field, and !~ is its opposite:

Terminal window
awk '/POST/ {print $9, $7}' access.log
awk '$7 ~ /^\/guides\// {print $7}' access.log
500 /api/search
200 /api/search
/guides/
/guides/awk/
/guides/jq/

Combine as many conditions as you like:

Terminal window
awk -F, '$4 == "keyboard" && $5 >= 2 {print $1, $3}' orders.csv
1001 Acme Ltd
1005 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.

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:

Terminal window
awk '{bytes += $10} END {print bytes}' access.log
awk '{bytes += $10} END {print bytes / NR}' access.log
177358
14779.8

Count lines that match, rather than adding a field:

Terminal window
awk '$9 == 404 {n++} END {print n + 0}' access.log
3

The + 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.

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:

Terminal window
awk '{count[$9]++} END {for (s in count) print s, count[s]}' access.log | sort
200 7
304 1
404 3
500 1

Total bytes by client, biggest first:

Terminal window
awk '{ bytes[$1] += $10 }
END { for (ip in bytes) print bytes[ip], ip }' access.log | sort -rn
130220 198.51.100.23
23552 192.0.2.10
22050 192.0.2.44
1536 203.0.113.5

It’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.

sort -u removes duplicate lines, and rearranges the rest in the process. awk can keep the original order:

Terminal window
awk '!seen[$1]++ {print $1}' access.log
192.0.2.10
198.51.100.23
203.0.113.5
192.0.2.44

Read 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.

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.

Terminal window
awk '{ if ($9 >= 500) s = "server error"
else if ($9 >= 400) s = "client error"
else s = "ok"
print $9, s, $7 }' access.log | head -7
200 ok /
200 ok /css/site.css
200 ok /guides/
404 client error /wp-login.php
404 client error /.env
200 ok /guides/awk/
500 server error /api/search

For 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:

Terminal window
awk -F, 'NR > 1 {print $1, $3, ($5 >= 3 ? "bulk" : "single")}' orders.csv
1001 Acme Ltd single
1002 Bloggs & Co bulk
1003 Acme Ltd bulk
1004 Initech single
1005 Bloggs & Co bulk
1006 Initech single
1007 Acme Ltd bulk

next 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.

Terminal window
awk -F, 'NR == 1 { next }
$4 == "keyboard" { kb += $5; next }
{ other += $5 }
END { print kb, "keyboards,", other, "other items" }' orders.csv
6 keyboards, 21 other items

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.

Terminal window
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.csv
Acme Ltd keyboard 258.00
Bloggs & Co keycaps 122.50
Acme Ltd cable 89.90
Initech keyboard 129.00
Bloggs & Co keyboard 387.00
Initech keycaps 49.00
Acme Ltd keycaps 98.00
Total 1133.40

That 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:

Terminal window
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 Initech

printf 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.

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:

Terminal window
awk "\$9 == $code" access.log # don't

This 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:

Terminal window
code=404
awk -v code="$code" '$9 == code {print $7}' access.log
/wp-login.php
/.env
/admin

For environment variables, there’s ENVIRON["HOME"].

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:

Terminal window
awk '{gsub(/"/, "", $6); print $6}' access.log | sort | uniq -c
10 GET
2 POST

split pulls a timestamp apart; here the hour and minute of the first three requests:

Terminal window
awk '{split($4, t, ":"); print t[2]":"t[3]}' access.log | head -3
09:14
09:14
09:15

print can redirect, inside awk, to a file named by any expression. Split the log into one file per status code:

Terminal window
awk '{print > ($9 ".log")}' access.log
cat 500.log
192.0.2.44 - - [17/Nov/2026:09:20:30 +0000] "POST /api/search HTTP/1.1" 500 0

The 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.

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:

Terminal window
awk -F, -v OFS=, 'NR > 1 {$6 = sprintf("%.2f", $6 * 1.1)} 1' \
orders.csv > orders.tmp && mv orders.tmp orders.csv

The 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.

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 prints a header, then one line per filesystem with the percentage used in the fifth column:

Terminal window
df -h | awk 'NR > 1 && $5+0 > 80'
backups 64.0K 56.0K 8.0K 88% /srv/backups

The +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 aux puts the PID in column 2, %CPU in column 3 and the command in column 11, on Linux and macOS alike:

Terminal window
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 numbers each line, so the command’s name is $2:

Terminal window
history | awk '{n[$2]++} END {for (c in n) print n[c], c}' |
sort -rn | head

Zsh’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 --numstat prints added and removed counts for each file, and two running totals turn that into one summary:

Terminal window
git diff --numstat | awk '{a += $1; d += $2} END {print "+" a, "-" d}'
+4 -1

Binary 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.

This one takes a different separator, and a loop over the fields:

Terminal window
echo "$PATH" | awk -F: '{for (i = 1; i <= NF; i++) print $i}'
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin

The 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 get pods prints NAME READY STATUS RESTARTS AGE, so:

Terminal window
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 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.

-F, splits on every comma, including those inside quoted fields:

Terminal window
echo '1007,2026-11-05,"Smith, Jones & Partners",cable,4,8.99' |
awk -F, '{print $3}'
"Smith

GNU 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.

They overlap, and the choice is mostly about which one makes the job a single obvious line:

  • cut for a fixed column on a single-character delimiter, and nothing else. It can’t handle runs of spaces, which rules out most command output, but cut -d, -f3 is hard to beat for a simple CSV.
  • sed for 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.

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.