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