title: Using sed to get the text between two key words (but not the key words themselves)
body: <p>So I have found this sed expression for getting the text between to keywords exclusive of the keywords:</p>
  
  <pre><code>cat example.txt | sed '/^KEYWORD1/,/^KEYWORD2/!d; //d'
  </code></pre>
  
  <p>where example.txt:</p>
  
  <pre><code>do
  not
  care
  KEYWORD1
  I
  want
  this
  KEYWORD2
  do
  not
  care
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>I
  want
  this
  </code></pre>
  
  <p>However, I want to understand for sure what is going on with this expression.  My understanding is that with a 'pattern range' (correct me if this is improper terminology), that a bool is set when you hit the first match and that the command(s) following the pattern range are only executed if that bool is true.</p>
  
  <p>Then there is the <code>//d</code> where the <code>//</code> is supposed to mean the last expression/regex that was matched.  So is it correct that in this case with a pattern range, the logic is as follows:</p>
  
  <ul>
  <li>loop</li>
  <li>Find <code>/^KEYWORD1/</code>, set bool to true, proceed with !d command which does not delete this line, then since the last regex was <code>/^KEYWORD1/</code> then <code>//d</code> is effectively <code>/^KEYWORD1/d</code> which deletes this line</li>
  <li>bool is true so it proceeds to not delete the 3 next lines and <code>/^KEYWORD1/</code> is not found on said lines so nothing is deleted</li>
  <li>Find <code>/^KEYWORD2/d</code>, execute !d and then <code>/^KEYWORD2/d</code> since this was the last regex used</li>
  </ul>
  
  <p>So at this point I'm not sure how the lines before and after are not printed since it doesn't execute the commands (the <code>!d</code>) unless the pattern range flag is set to true.</p>
  
  <p>Or does sed at least look at the command for every line and since the first command is an inverse delete, it somehow changes the logic to delete all other lines where the pattern range bool is false?</p>
  
  <p>Any clarification on how this sed expression works will be appreciated.  I've read <a href="http://www.grymoire.com/Unix/Sed.html" rel="nofollow">this great resource</a> up and down  but still don't quite understand all the details of the expression.</p>