Skip to content
# Files, text & logs

Text processing: grep, sed and awk

Search inside files, edit them in place, and pull columns out of structured text.

grep: search inside files

grep prints lines matching a pattern. -r searches recursively, -i ignores case, -n shows line numbers, -v inverts the match (lines that don't match).

bash
grep -rni 'error' /var/log/app/
grep -v '^#' /etc/ssh/sshd_config

sed: find and replace

sed streams a file through find/replace or other edits. -i edits the file in place; always test without -i first, or with -i.bak to keep a backup, since sed happily overwrites without confirmation.

bash
sed 's/foo/bar/g' file.txt          # preview
sed -i.bak 's/foo/bar/g' file.txt   # edit in place, keep file.txt.bak

awk: work with columns

awk splits each line into fields ($1, $2, ...) on whitespace by default. It's the fastest way to pull a specific column out of command output like ps, df or a log file.

bash
df -h | awk '{print $1, $5}'
awk -F: '{print $1}' /etc/passwd

Chaining them together

These tools are built to pipe into each other: grep to narrow lines, awk to pull a column, sort/uniq -c to count occurrences β€” a common combo for summarizing log data without writing a script.

bash
grep 'GET' access.log | awk '{print $7}' | sort | uniq -c | sort -rn | head