[sed] Insert a line before each matching line

Created on November 30, 2026

Use sed's i command to insert text immediately before a matching line. This example inserts orange before banana.

printf 'apple\nbanana\n' | sed '/banana/i\
orange'
apple
orange
banana

/banana/ is the address pattern. A backslash after i lets the next line supply the inserted text.

Bash Shell command sed