awk + sed Mastery

awk is best for column-based parsing and conditional reporting. sed is best for quick line editing, replacements and stream transformations.

awk Essentials

awk '{print $1}' access.log
awk '{print $1, $4, $9}' access.log
awk -F',' '{print $1, $3}' users.csv
awk '$9 >= 500 {print $0}' access.log
awk '/ERROR|WARN/' app.log
awk 'NR==1,NR==20 {print}' app.log
awk '{count[$1]++} END {for (k in count) print k, count[k]}' access.log

sed Essentials

sed 's/localhost/127.0.0.1/g' config.yml
sed -n '1,20p' file.txt
sed '/DEBUG/d' app.log
sed -n '/BEGIN/,/END/p' file.txt
sed 's/[[:space:]]\+$//' file.txt
sed -i 's/old/new/g' file.txt

Practical Combinations

grep ERROR app.log | awk '{print $1, $2, $NF}'
ps aux | awk '{print $2, $3, $4, $11}' | head
df -h | awk 'NR==1 || $5+0 > 80'
sed -n '1000,1050p' app.log | grep Exception

When to Use Which

NeedBetter Tool
Pick columns from structured textawk
Filter by numeric conditionsawk
Replace text quicklysed
Delete or print line rangessed
Summarize countsawk