Inserting Text Before a Certain Line
(taken from SO)
$ cat animals dog cat dolphin cat $ sed "/cat/ { N; s/cat\n/giraffe\n&/ }" animals dog giraffe cat dolphin cat
sed
explained:
- match a line with
/cat/
- continue on next line (
N
) - substitute matched pattern with the insertion and the matched string where
&
represents the matched string