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.txtPrint first column
awk '{print $1, $3}' file.txtPrint columns 1 and 3
awk '{print $NF}' file.txtPrint last column ($NF = number of fields)
awk '{print NR, $0}' file.txtPrint line number and whole line
awk -F: '{print $1}' /etc/passwdUse : as separator
awk -F, '{print $2}' data.csvCSV processing
awk '$3 > 100' file.txtLines where col3 > 100
awk '/pattern/' file.txtLines matching pattern
awk 'NR==5' file.txtLine 5 only
awk 'NR>=2 && NR<=5' file.txtLines 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 -lOnly the lines whose 9th field is 404
awk '{print $1}' ~/logs/access.log | sort | uniq -c | sort -rn | head -3Requests per address, most first
awk -F, 'NR > 1 {print $2}' ~/data/sales.csvNR > 1: skip line 1
awk -F, 'NR > 1 {t += $3 * $4} END {print t}' ~/data/sales.csvA 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.csvOne 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
- sort & uniq — Sorting and Deduplication
- awk — Column Processing
- Reading the Logs
- Project: Daily Log Report
- Project: Sales Report from a CSV
- Project: Server Health Check