title: SED: Delete X Lines After a Match
body: <p>I'm trying to delete a range using <code>sed</code>.  The range is from a known match and the next 2 lines.  Let's say I want to delete all lines that start with <code>Don't</code> and then the 2 lines that follow.  </p>
  
  <p>Please note that I am also doing substitutions in a sed command file.  As such, I am avoiding using <code>-n</code> and <code>/p</code> in my solution space.  </p>
  
  <p>For whatever reason, I want to limit myself to one invocation of sed.</p>
  
  <p>Here's my data (data.txt):</p>
  
  <pre><code>Print Me
  Please Output This line
  Don't Print Me and 2 more lines
  This line is no good
  So is this one
  We should see this line
  And this one, too.
  </code></pre>
  
  <p>Here's my expected output:</p>
  
  <pre><code>Print Me
  Please Output This line
  We should see this line
  And this one, too.
  </code></pre>
  
  <p>Here's an attempted go at this:</p>
  
  <pre><code>sed -f delete_dont_plus_2.sed data.txt
  </code></pre>
  
  <p>With this as <code>delete_dont_plus_2.sed</code>:</p>
  
  <pre><code>/^Don't/,+2d
  </code></pre>
  
  <p>Here is my result:</p>
  
  <pre><code>sed: 1: delete_dont_plus_2.sed: expected context address
  </code></pre>
  
  <p>I've also tried these:</p>
  
  <pre><code>/^Don't/,/^Don't/+2d
  /^Don't/,{/^Don't/+2}d
  </code></pre>
  
  <p>Second approach to this question:</p>
  
  <p>Let's say we wanted to make this code a little more robust.  Today there are 2 more lines that need to be deleted, but who knows how many lines there will be in the future.  Let's say we want to delete up to, but not including <code>We should see this line</code>.  In this variant of the question, the results are exactly the same.  Again, let's consider a limited BSD sed so we cannot use an expression like <code>/^Don't/,/^We should see this line/-1d</code>.</p>
  
  <p>Thanks!</p>