Text Processing: grep, sed, awk

Advanced

Overview

grep filters lines that match a pattern. Combine it with pipes to drill into logs quickly, and use -i for case-insensitive and -r to search recursively.

sed edits streams: the s/old/new/g form does find-and-replace. awk treats each line as fields split by whitespace, perfect for columnar data like ps or CSV output.

These tools shine when chained together with | so the output of one becomes the input of the next.

Cheatsheet

grep -i error app.logCase-insensitive search
grep -rn TODO src/Recursive search with line numbers
sed 's/foo/bar/g' fReplace all foo with bar
awk '{print $1}' fPrint the first column
awk -F, '{print $2}' fUse comma as the field separator
sort | uniq -cCount unique lines

Try it

A safe, simulated terminal. Run the suggested commands to see typical output.

simulated terminal

Type a command and press Enter, or click a suggestion below to run it.

$

Quick quiz

1. Which command replaces every 'foo' with 'bar' in a stream?

2. By default, awk splits each line into fields based on what?