title: bash-delete if string exists between two patterns
body: <p>I've been trying to use <code>sed</code> to accomplish the following. Let's say I have the following file (note: my actual file is more complicated than this):</p>
  
  <pre><code>hello world
  foo bar
  people people
  target
  something
  done
  </code></pre>
  
  <p>I want to check if <code>target</code> exists between two patterns, in this example, between lines <code>foo bar</code> and <code>done</code> (both lines inclusive), and delete the whole pattern if the <code>target</code> does exist.</p>
  
  <p>I know how to delete the lines between the two patterns using this <code>sed</code> command:</p>
  
  <pre><code>sed '/people.*/,/done/d' file
  </code></pre>
  
  <p>But I want only to delete it if the string <code>target</code> exists in between the two string matches.</p>
  
  <p>My logic has been something like this:</p>
  
  <pre><code>sed -n '/people.*/,/done/p' file | check if target string exists | delete entire pattern found by sed
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I forgot to mention that there can be any number of words before <code>target</code> and after <code>target</code> on the same line.</p>