Text Processing & Shell Tools
Text Processing & Shell Tools
7 interactive questions
`grep` vs `awk` vs `sed` — when to use each?
grep — filter lines matching a pattern. awk — field-based processing, columns, math, structured text. sed — stream editing, in-place changes, simple substitutions. 🎯 awk for CSV/column data; sed for line edits; grep for search.
How do you extract column 3 from a CSV with awk?
awk -F',' '{print $3}' file.csv. For header skip: awk -F',' 'NR>1 {print $3}'.
What does `xargs` do?
Builds and executes command lines from standard input. Converts input into arguments. find . -name "*.log" | xargs rm deletes them. -I {} placeholder: xargs -I {} cp {} /backup/. ⚠️ Use -0 with find -print0 for filenames with spaces.
How do you replace all occurrences of a string in files recursively?
sed -i 's/old/new/g' $(grep -rl 'old' .) or find . -type f -exec sed -i 's/old/new/g' {} +.
`cut` vs `awk` for column extraction?
cut -d',' -f2 is simpler for single-char delimiters. awk -F',' '{print $2}' handles multi-char delimiters, regex, and complex logic.
How do you sort and deduplicate?
sort file | uniq (adjacent dedup). sort -u file (sort + unique). sort -k2,2 -n sorts numerically by column 2.
What is `find` and useful predicates?
Recursively searches files. -name "*.log", -type f (files), -mtime -7 (modified <7 days), -size +100M, -exec cmd {} +, -delete. ⚠️ -exec runs once per file; -exec ... + batches.