[sed] Print only lines where a substitution succeeds

Created on November 30, 2026

Combine sed -n and the substitution flag p to print just the lines changed by a substitution.

printf 'apple\nbanana\napple pie\n' | sed -n 's/apple/orange/p'
orange
orange pie

-n suppresses normal printing. The p flag prints a line when the replacement succeeds, so the banana line is omitted.

Bash Shell command sed