I need to add a line with bar
before each line with foo
with sed
.
I need to do this in a Makefile and so I cannot use i\
because it needs a newline in standard sed
(not GNU sed, e.g., the one in Mac OS X) and this cannot be done in a Makefile (at least, not nicely).
The solution I found is:
sed '/foo/{h;s/.*/bar/;p;g;}' < in > out
This saves the line, replaces its contents with bar
, prints the new line, restores the old line (and prints it by default).
Is there a simpler solution?