title: awk / sed : replacing a pattern having newline character inside it with a string
body: <p>Files looks like these. I am running a <code>sed</code>in a loop for ALL files in a dir. 
  They have variable patterns like so </p>
  
  <p>Patterns 1 <br></p>
  
  <pre><code>colx ) AS 
  ( rightsaidfred, 
  </code></pre>
  
  <p>2<br></p>
  
  <pre><code>coly )     AS 
      ( rightsaidfred,
  </code></pre>
  
  <p>3<br></p>
  
  <pre><code>    colz)
  AS 
          ( rightsaidfred,
  </code></pre>
  
  <p>In all cases 
  I want to replace like so </p>
  
  <pre><code>colN  , col1 ) 
  AS 
  ( rightsaidfred,
  </code></pre>
  
  <p>I DONT have GNU <code>sed</code>. Just the standard POSIX <code>sed</code></p>
  
  <p><code>sed 's/)*AS/col1,) \
   AS /g' file</code></p>
  
  <p>does not work. Note having no GNU <code>sed</code>  <code>-i</code> option is out . If I want to newline char I have to continue with '\' and then press <code>enter</code> like above but the above gives weird results. Actually previous col name is variable 
  Its not the same  <br> just to clarify Col1 col2 ..ColN are varied names. Keep a track of these names is cumbersome which is what the <code>awk</code> is trying to do.All I want is for some utility out there to see if this pattern <code>)*AS</code>  (where * can include a <code>\n</code> char  i.e. </p>
  
  <pre><code> ) 
  AS
  </code></pre>
  
  <p>Exists and Replace is by  <code>, col1 ) AS</code>  i.e. I just added a comma and a col1 in front of the <code>)</code> bracket. That is what I am trying to do .
  In that context I tried using  the<code>%</code> in the  <code>sed</code> replace but it will NOT work.<br>
  <code>sed 's/pattern/&amp;replacement&amp;/g' file</code>
  does not work. I cannot get it to recognize </p>
  
  <pre><code> colx ) 
  as 
  </code></pre>
  
  <p>pattern where  <code>as</code> comes to next line 
      (</p>
  
  <p>Input <br></p>
  
  <pre><code> col_frank ) as ( col_peter 
   col_oliver )        as       ( col_north
   col_dave ) 
  as 
  ( col_sam 
  </code></pre>
  
  <p>Output </p>
  
  <pre><code>   col_frank,col1 ) as ( col_peter 
       col_oliver,col1 )        as       ( col_north
       col_dave,col1 ) 
      as 
      ( col_sam 
  </code></pre>
  
  <p><br>
  Note :  <em>all Col_names</em> , names like Col_frank , col_peter are <em>variables</em> and non constants. The <strong>only</strong> thing constant here is the <code>)*as</code> pattern. The pattern spelt out is <br>  <code>)  followed by a boatload of anything including a newline char , followed by "as"</code> <br>
  The "as" could be case-insensitive. This is actually previously home grown code that I am modifying . Some AS strings are ucase others are Lcase. </p>
  
  <p><br> This is on AIX. 
  <br></p>