Tools › Cheat sheets › Linux & Shell › awk
🧮

awk cheat sheet

awk splits each line into fields ($1, $2, ... with $0 the whole line and NF the number of fields) and runs your code on lines that match. These examples work with GNU awk and most others.

Click any command to copy it.

Print columns

CommandWhat it does
awk '{print $1}' filePrint the first column.
awk '{print $1, $3}' filePrint columns 1 and 3.
awk '{print $NF}' filePrint the last column.
awk -F: '{print $1}' /etc/passwdUse a custom separator (here a colon).
awk -F, -v OFS=';' '{print $1, $2}' file.csvRead comma-separated, write semicolon-separated.
awk '{print NR": "$0}' fileNumber each line.
awk '{$2=""; print}' filePrint everything except column 2.

Filter rows

CommandWhat it does
awk 'NR==5' filePrint line 5.
awk 'NR>=2 && NR<=4' filePrint lines 2 to 4.
awk '/error/' filePrint lines matching a pattern.
awk '$3 > 100' filePrint lines where column 3 is greater than 100.
awk '$1=="alice" {print $2}' filePrint column 2 where column 1 equals alice.
awk 'length($0) > 80' filePrint lines longer than 80 characters.
awk 'NF' fileSkip empty lines.

Calculate

CommandWhat it does
awk '{sum+=$1} END {print sum}' fileAdd up column 1.
awk '{sum+=$1} END {print sum/NR}' fileAverage of column 1.
awk 'NR==1{max=$1} $1>max{max=$1} END{print max}' fileMaximum of column 1.
awk '{count[$1]++} END {for (k in count) print k, count[k]}' fileCount occurrences of each value in column 1.
awk '{a[$1]+=$2} END {for (k in a) print k, a[k]}' fileSum column 2 grouped by column 1.

Handy one-liners

CommandWhat it does
awk '!seen[$0]++' fileRemove duplicate lines, keeping order.
awk '{print toupper($0)}' fileConvert to uppercase.
awk '{gsub(/old/,"new"); print}' fileReplace all occurrences on each line.
awk '{print substr($0, 1, 10)}' filePrint the first 10 characters of each line.
awk 'BEGIN {print "Header"} {print} END {print "Total:", NR}' fileAdd a header and a footer.
awk '{printf "%-10s %5d\n", $1, $2}' fileFormat output in aligned columns.
ps aux | awk '$3 > 50 {print $2, $11}'Processes using more than 50% CPU.

Frequently asked questions

What do NR, NF and $0 mean?

NR is the current line number, NF is the number of fields on the line and $0 is the entire line.

How do I use a variable from the shell in awk?

Pass it with -v: awk -v min=100 '$3 > min' file.

More Linux & Shell cheat sheets

🐚 Bash🔎 grep🔍 find✂️ sed📦 tar📝 Vim🪟 tmux🔐 chmod permissionsAll Linux & Shell cheat sheets →
Copied to clipboard