awk Commands

awk commands cheatsheet.

Pull out columns, filter rows and do quick maths on text. Tap any command to copy it.

Print columns
awk '{print $1}' file.txtPrint the first field of each line
awk '{print $1, $3}' file.txtPrint fields 1 and 3
awk '{print $NF}' file.txtPrint the last field on each line
awk '{print NR, $0}' file.txtPrefix each line with its line number
awk -F',' '{print $2}' file.csvUse a comma as the field separator
awk -F':' '{print $1}' /etc/passwdSplit on colons (print usernames)
Filter rows
awk '/pattern/' file.txtPrint lines matching a pattern
awk '$3 > 100' file.txtPrint rows where field 3 exceeds 100
awk 'NR==1' file.txtPrint just the first line (the header)
awk 'NR>1' file.txtSkip the header line
awk 'length > 80' file.txtPrint lines longer than 80 characters
awk 'NF' file.txtPrint only non-empty lines
Compute & dedupe
awk '{sum += $1} END {print sum}' fSum the first column
awk '{print $1+$2}' file.txtAdd two columns per line
awk 'END {print NR}' file.txtCount lines (like wc -l)
awk '!seen[$0]++' file.txtRemove duplicate lines, keep the order
awk '{a+=$1} END {print a/NR}' fAverage the first column
$1, $2… are the fields (columns) of each line, $0 is the whole line, $NF is the last field, NR is the current line number, and NF is the number of fields. Set the separator with -F.

awk sees text as columns

Where grep matches lines and sed edits them, awk treats each line as a set of fields — columns split on whitespace by default. $1 is the first column, $2 the second, and $NF the last (NF being the number of fields). So awk '{print $1}' pulls the first column out of any whitespace-separated data — log files, command output, tables — and awk '{print $1, $NF}' grabs the first and last. For CSVs or /etc/passwd, change the separator with -F',' or -F':'.

Filtering rows by condition

An awk program is a series of pattern { action } pairs: for each line, if the pattern is true, the action runs. Leave out the action and awk prints matching lines, so awk '$3 > 100' prints every row whose third column is over 100 — numeric comparisons that grep can't do. The built-in variable NR (record number) makes header handling trivial: NR>1 skips the first line, and NR==1 keeps only it.

Quick maths and de-duping

awk's END block runs once after all lines are read, which is perfect for totals: awk '{sum += $1} END {print sum}' adds up a column, and dividing by NR gives an average — a one-liner that replaces opening a spreadsheet. One famous idiom, awk '!seen[$0]++', removes duplicate lines while preserving their original order, something sort -u can't do. When you need to edit text rather than analyse it, reach for sed; to filter before processing, pipe through grep.

FAQ

How do I print a specific column with awk?
Use awk '{print $2}' file to print the second column, where columns are split on whitespace. Use $NF for the last column, and -F',' to split on commas instead.
How do I change the field separator in awk?
Pass -F followed by the separator, like awk -F',' '{print $1}' for CSVs or awk -F':' for /etc/passwd. The fields $1, $2… are then split on that character.

More cheatsheets