awk splits each line into fields $1, $2, … ($0 is the whole line,
$NF the last field). NR is the line number, NF the number of fields.
The pattern { action } runs the action on lines matching the pattern.
Command
What it does
awk '{print $1}' access.log
Print field 1
awk '{print $1, $9}' access.log
Fields 1 and 9, space-separated
awk -F, '{print $2}' data.csv
Set the input separator
awk -F, -v OFS='\t' '{print $1,$3}' data.csv
Set the output separator
awk '$9 >= 500' access.log
Lines where field 9 ≥ 500
awk '$9 == 404 {print $7}' access.log
Paths that returned 404
awk '/ERROR/ {n++} END {print n}' app.log
Count matching lines
awk '{s += $10} END {print s}' access.log
Sum a column
awk '{s += $1} END {print s/NR}' nums.txt
Average
awk 'NR > 1' data.csv
Skip the header
awk 'length > 120' file
Lines longer than 120 characters
awk '{print NR": "$0}' file
Number the lines
awk -F, '{sum[$2] += $3} END {for (k in sum) print k, sum[k]}' data.csv
# 1. Top 10 client IPs in a web logawk'{print $1}'access.log|sort|uniq-c|sort-rn|head-n10# 2. Count of each HTTP status codeawk'{print $9}'access.log|sort|uniq-c|sort-rn
# 3. Requests per hourawk'{split($4, t, ":"); print t[2]}'access.log|sort|uniq-c
# 4. All 5xx errorsawk'$9 >= 500'access.log
# 5. Most requested pathsawk'{print $7}'access.log|sort|uniq-c|sort-rn|head
# 6. Total bytes servedawk'{s += $10} END {print s}'access.log
# 7. Unique IPv4 addresses anywhere in a filegrep-oE'([0-9]{1,3}\.){3}[0-9]{1,3}'access.log|sort-u
# 8. Ten most common words in a texttr-cs'[:alpha:]''\n'<book.txt|tr'A-Z''a-z'|sort|uniq-c|sort-rn|head
# 9. Strip comments and blank lines from a config filegrep-vE'^\s*(#|$)'/etc/ssh/sshd_config
# 10. Count rows per value of CSV column 2 (skipping the header)tail-n+2data.csv|cut-d,-f2|sort|uniq-c|sort-rn
# 11. Remove duplicate lines but keep the original orderawk'!seen[$0]++'list.txt
# 12. Find which files contain a string, then count matches per filegrep-rc'TODO'--include='*.py'.|grep-v':0$'|sort-t:-k2,2nr
# 13. Replace a string in every matching file (preview first without -i)grep-rl'old.example.com'conf/|xargssed-i's/old\.example\.com/new.example.com/g'# 14. Lines in a.txt that are not in b.txtcomm-23<(sorta.txt)<(sortb.txt)# 15. Watch the error count in a log, refreshed every 5 secondswatch-n5"grep -c ERROR /var/log/app.log"