The awk command

pattern scanning and processing language 27 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.

Examples

history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10
cat access.log | awk '{print $1}' | sort | uniq -c | sort -rn
awk 'pattern { action }' file
awk '{print $1}' file.txt

Print first column

awk '{print $1, $3}' file.txt

Print columns 1 and 3

awk '{print $NF}' file.txt

Print last column ($NF = number of fields)

awk '{print NR, $0}' file.txt

Print line number and whole line

awk -F: '{print $1}' /etc/passwd

Use : as separator

awk -F, '{print $2}' data.csv

CSV processing

awk '$3 > 100' file.txt

Lines where col3 > 100

awk '/pattern/' file.txt

Lines matching pattern

awk 'NR==5' file.txt

Line 5 only

awk 'NR>=2 && NR<=5' file.txt

Lines 2-5

awk '{sum += $3} END {print sum}' file.txt
awk -F: '{print $1, $3}' /etc/passwd
awk '{sum+=$2; count++} END {print sum/count}' data.txt
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -3
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c
awk '$9 == 404 {print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
awk '$9 == 404' ~/logs/access.log | wc -l

Only the lines whose 9th field is 404

awk '{print $1}' ~/logs/access.log | sort | uniq -c | sort -rn | head -3

Requests per address, most first

awk -F, 'NR > 1 {print $2}' ~/data/sales.csv

NR > 1: skip line 1

awk -F, 'NR > 1 {t += $3 * $4} END {print t}' ~/data/sales.csv

A running total, printed at the END

awk -F, 'NR > 1 {r[$2] += $3 * $4} END {for (p in r) print p, r[p]}' ~/data/sales.csv

One total per product

df / | awk 'NR == 2 {print $5}'

8%: the "Use%" column of the line for /

df / | awk 'NR == 2 {print $5}' | tr -d %

8: just the number

free -m | awk '/Mem/ {print $3}'

Memory used, in MB

Lessons that use awk

Related commands