The s/from/to/ engine
sed applies a small program to each line of input and prints the result. The most common command is s/pattern/replacement/ — substitute. Once you've used this, you'll never copy-paste-find-replace in a GUI editor again.
Substitution
sed 's/foo/bar/'— first occurrence per line.sed 's/foo/bar/g'— every occurrence (global).sed 's/foo/bar/I'— case-insensitive (GNU).sed 's|/usr/local|/opt|g'— change delimiter when substituting paths (any single char works).sed -E 's/(foo|bar)/[\1]/g'— extended regex with capture groups;\1is the first captured group.
Other commands
sed '/pattern/d'— delete matching lines.sed -n '5,10p' file— print only lines 5-10 (-n suppresses default output).sed '$d' file— delete the last line.sed '1i\\ header' file— insert text at line 1.
In-place editing
sed -i rewrites the file. BSD sed (macOS) requires an empty argument: sed -i '' 's/foo/bar/g' file. GNU sed: sed -i 's/foo/bar/g' file. The mismatch trips up everyone the first time. brew install gnu-sed + alias if you want consistency.
Sed vs awk vs perl
sed for line-by-line substitutions. awk for field-aware processing. perl -pe for serious regex (full PCRE with lookaheads). Each has its place; sed is the smallest and the most portable.