After this lesson, you will be able to: Use awk, sed, and cut to extract, transform, and reformat text, the foundation of every Unix log-analysis workflow.
Logs come in lines; analysis happens with text tools. awk + sed + cut are the three you'll reach for daily.
Fastest way to pull a column out of structured text.
# Extract usernames from /etc/passwd (colon-separated)cut -d: -f1 /etc/passwd# -d sets delimiter, -f selects field(s)cut -d: -f1,3 /etc/passwd # multiple fieldscut -d',' -f2- data.csv # field 2 onwardcut -c1-10 file.txt # first 10 characters per line
awk is a full programming language for text. Most people use 5% of it; that's enough.
# Print the 7th column (the user name) from ps aux outputps aux | awk '{print $7}'# Field separator (default is whitespace)awk -F: '{print $1, $3}' /etc/passwd# Conditional: lines where column 3 > 1000awk -F: '$3 > 1000 {print $1}' /etc/passwd# Sum column 5awk '{sum += $5} END {print sum}' /var/log/access.log# Count unique values in column 7awk '{print $7}' access.log | sort | uniq -c | sort -rn | head# NR = current line number; NF = number of fields on current lineawk 'NR==10' file.txt # 10th lineawk 'NF > 5' file.txt # lines with more than 5 fields
sed is what you reach for 'I need to replace X with Y across this file/stream'.
# Replace 'foo' with 'bar' (first occurrence per line)sed 's/foo/bar/' file.txt# Replace ALL occurrences (g = global)sed 's/foo/bar/g' file.txt# In-place edit (BSD vs GNU difference: BSD/macOS needs `sed -i ''`)sed -i 's/foo/bar/g' file.txt# Delete lines matching a patternsed '/^#/d' config.ini # delete comment linessed '/^$/d' file.txt # delete blank lines# Insert / append / change a linesed '5a\\nNew line after line 5' file.txtsed '1i\\nFirst line inserted' file.txt# Multiple expressionssed -e 's/foo/bar/g' -e 's/baz/qux/g' file.txt
JSON: use `jq`. `cat data.json | jq '.users[].name'`. Trying to parse JSON with awk / sed is a rite of passage and a mistake. YAML: use `yq` (same syntax as jq, but for YAML). Code search across a tree: use `ripgrep` (rg). Respects .gitignore by default; way faster than `grep -r`. Knowing when to put awk/sed away and switch to a structured tool is a senior-engineer move.
Writing 20-line awk programs instead of using Python. If it's complex, escape to Python. Using sed on JSON. Always wrong; use jq. Forgetting that sed -i differs between BSD/macOS and GNU/Linux. Scripts that work on Mac break on Linux (and vice versa). Not piping `head` after exploratory commands. Don't dump a million-line file to the terminal. Conflating awk's `==` (compare) with `=` (assign).
Pick the cleanest explanation.
Sign in and purchase access to unlock this lesson.