title: sed with multiple conditions
body: <p>I'd like to have your kind help on the matter of using <code>sed</code> command for searching/replacing the text in file with some specific purposes below.
  <br>Supposing I'm having <code>/tmp/foo</code> with following content.</p>
  
  <pre><code>aaa whatever_a1 KEYWORD_1 whatever_a2
  bbb whatever_b1 whatever_b2
  ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
  aaa whatever_aaa1 whatever_aaa2
  aaa whatever_aaa1 whatever_aaa2 KEYWORD_1 KEYWORD_2
  ddd whatever_d1 whatever_d2 KEYWORD_1
  </code></pre>
  
  <p>The return should have as following:
  <br><br>1. find /tmp/foo which lines contains <code>aaa whatever_a1 KEYWORD_1 whatever_a2</code> >>> replace line to <code>Hello World</code></p>
  
  <pre><code>Hello World
  bbb whatever_b1 whatever_b2
  ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
  aaa whatever_aaa1 whatever_aaa2
  aaa whatever_aaa1 whatever_aaa2 KEYWORD_1 KEYWORD_2
  ddd whatever_d1 whatever_d2 KEYWORD_1
  </code></pre>
  
  <p><br>2. find /tmp/foo which lines begins with <code>aaa</code> >>> replace line to <code>Hello World</code></p>
  
  <pre><code>Hello World
  bbb whatever_b1 whatever_b2
  ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
  Hello World
  Hello World
  ddd whatever_d1 whatever_d2 KEYWORD_1
  </code></pre>
  
  <p><br>3. find /tmp/foo which lines contains <code>aaa</code> AND <code>KEYWORD_1</code> >>> replace line to <code>Hello World</code></p>
  
  <pre><code>Hello World
  bbb whatever_b1 whatever_b2
  ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
  aaa whatever_aaa1 whatever_aaa2
  Hello World
  ddd whatever_d1 whatever_d2 KEYWORD_1
  </code></pre>
  
  <p><br>4. find /tmp/foo which lines contains <code>aaa</code> at beginning AND <code>KEYWORD_1</code> >>> replace line to <code>Hello World</code></p>
  
  <pre><code>Hello World
  bbb whatever_b1 whatever_b2
  ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
  aaa whatever_aaa1 whatever_aaa2
  Hello World
  ddd whatever_d1 whatever_d2 KEYWORD_1
  </code></pre>
  
  <p><br>5. find /tmp/foo which lines contains <code>aaa</code> AND <code>KEYWORD_1</code> >>> replace <code>KEYWORD_1</code> to <code>NEW_TEXT</code> only</p>
  
  <pre><code>aaa whatever_a1 NEW_TEXT whatever_a2
  bbb whatever_b1 whatever_b2
  ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
  aaa whatever_aaa1 whatever_aaa2
  aaa whatever_aaa1 whatever_aaa2 NEW_TEXT KEYWORD_2
  ddd whatever_d1 whatever_d2 KEYWORD_1
  </code></pre>
  
  <p><br>6. find /tmp/foo which lines contains <code>aaa</code> AND <code>KEYWORD_1</code> >>> purge <code>KEYWORD_1</code> text only</p>
  
  <pre><code>aaa whatever_a1  whatever_a2
  bbb whatever_b1 whatever_b2
  ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
  aaa whatever_aaa1 whatever_aaa2
  aaa whatever_aaa1 whatever_aaa2  KEYWORD_2
  ddd whatever_d1 whatever_d2 KEYWORD_1
  </code></pre>
  
  <p><br>7. find /tmp/foo which lines contains <code>aaa</code> AND <code>KEYWORD_1</code> >>> replace string behind <code>KEYWORD_1</code> to <code>Hello World</code> only (keep <code>KEYWORD_1</code> in line)</p>
  
  <pre><code>aaa whatever_a1 KEYWORD_1 Hello World
  bbb whatever_b1 whatever_b2
  ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
  aaa whatever_aaa1 whatever_aaa2
  aaa whatever_aaa1 whatever_aaa2 KEYWORD_1 Hello World
  ddd whatever_d1 whatever_d2 KEYWORD_1
  </code></pre>
  
  <p><br>8. find /tmp/foo which lines contains <code>aaa</code> AND <code>KEYWORD_1</code> >>> replace string front of KEYWORD_1 to <code>Hello World</code> only (keep <code>KEYWORD_1</code> in line)</p>
  
  <pre><code>Hello World KEYWORD_1 whatever_a2
  bbb whatever_b1 whatever_b2
  ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
  aaa whatever_aaa1 whatever_aaa2
  Hello World KEYWORD_1 KEYWORD_2
  ddd whatever_d1 whatever_d2 KEYWORD_1
  </code></pre>
  
  <p><br>9. find /tmp/foo which lines contains <code>aaa</code> AND <code>KEYWORD_1</code> >>> replace string behind <code>KEYWORD_1</code> to <code>Hello World</code> only (including <code>KEYWORD_1</code> text)</p>
  
  <pre><code>aaa whatever_a1 Hello World
  bbb whatever_b1 whatever_b2
  ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
  aaa whatever_aaa1 whatever_aaa2
  aaa whatever_aaa1 whatever_aaa2 Hello World
  ddd whatever_d1 whatever_d2 KEYWORD_1
  </code></pre>
  
  <p><br>10. find /tmp/foo which lines contains <code>aaa</code> AND <code>KEYWORD_1</code> >>> replace string front of <code>KEYWORD_1</code> to <code>Hello World</code> only (including KEYWORD_1 text)</p>
  
  <pre><code>Hello World whatever_a2
  bbb whatever_b1 whatever_b2
  ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
  aaa whatever_aaa1 whatever_aaa2
  Hello World KEYWORD_2
  ddd whatever_d1 whatever_d2 KEYWORD_1
  </code></pre>
  
  <p><br>11. find /tmp/foo which lines contains <code>aaa</code> AND <code>KEYWORD_1</code> AND <code>KEYWORD_2</code> >>> replace line to <code>Hello World</code></p>
  
  <pre><code>aaa whatever_a1 KEYWORD_1 whatever_a2
  bbb whatever_b1 whatever_b2
  ccc whatever_c1 whatever_c2 KEYWORD_1 whatever_c3
  aaa whatever_aaa1 whatever_aaa2
  Hello World
  ddd whatever_d1 whatever_d2 KEYWORD_1
  </code></pre>
  
  <p>Thanks a lot,
  <br>Cuong</p>