title: sed two pattern search and append line after the last occurence of the second pattern
body: <p>The following <code>sed</code> question has been giving me a lot of headaches over the last 2 days.</p>
  
  <p>I have the following file <em>(iptables)</em>:</p>
  
  <pre><code>someline
  
  someline
  
  *filter
  
  :INPUT ACCEPT [0:0]
  
  :FORWARD ACCEPT [0:0]
  
  :OUTPUT ACCEPT [0:0]
  
  someline
  </code></pre>
  
  <p>I want to use <code>sed</code> to search for the pattern <code>*filter</code> and if matched then search for a second pattern <code>[0:0]</code> and then add a line after the last occurrence of the second pattern.</p>
  
  <p>This would then (ideally) lead to:</p>
  
  <pre><code>someline
  
  someline
  
  *filter
  
  :INPUT ACCEPT [0:0]
  
  :FORWARD ACCEPT [0:0]
  
  :OUTPUT ACCEPT [0:0]
  
  TESTLINEADDEDBYSED
  
  someline
  </code></pre>
  
  <p>The forum here had given me a good start, but I just can't crack this specific issue.</p>
  
  <p>I so far have the following solution, but it adds the line after the first occurrence of the second pattern <code>[0:0]</code>:</p>
  
  <pre><code>sed -n '/\*filter/{:a;N;/^\n/s/^\n//;/\[0:0\]/{!ba;p;s/.*/TESTLINEADDEDBYSED/;};ba}; p' file
  </code></pre>
  
  <p>I was under the assumption that with <code>.*</code> in the second branch I would consume (read: skip?) the first occurrence of <code>[0:0]</code>?
  What am I misunderstanding here?</p>