title: Output text between two patterns on the same line using sed command
body: <p>I tried this command, but it works only to a certain extent.</p>
  
  <p>Input file content:</p>
  
  <pre><code>this is begin not sure what is wrong end and why not
  </code></pre>
  
  <p>Command:</p>
  
  <pre><code>cat file | sed 's/.*begin \(.*\)end/\1/'
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>not sure what is wrong and why not
  </code></pre>
  
  <p>Desired output (please see notes below):</p>
  
  <pre><code>not sure what is wrong 
  </code></pre>
  
  <ol>
  <li>My sed command searches for the first pattern and second pattern, but omits the second pattern and prints the text. However, it also prints the rest of the line, <code>why not</code>. I do not want to print what comes after the second pattern, only what is between the two patterns. I am not sure how to do this.</li>
  <li>What if there are two <code>end</code> on the same line?</li>
  </ol>
  
  <p>Can someone provide and explain the command?</p>