Printing a text file in a patteren
<p>I have a very large amount of text(100000 words)  to parse and it has got following format </p>

<pre><code>abrasion abrade:stem&lt;&gt;ion:suffix
abstainer abstain:stem&lt;&gt;er:suffix
abstention abstain:stem&lt;&gt;ion:suffix
accessibility access:stem&lt;&gt;ible:suffix&lt;&gt;ity:suffix
accretion accrete:stem&lt;&gt;ion:suffix
activist active:stem&lt;&gt;ist:suffix
adaptable adapt:stem&lt;&gt;able:suffix
addiction addict:stem&lt;&gt;ion:suffix
</code></pre>

<p>I need to convert it to this format</p>

<pre><code>abrasion (S (abrade:stem) (ion:suffix))
abstainer (S (abstain:stem) (er:suffix))
abstention (S (abstain:stem) (ion:suffix))
accessibility (S (S (access:stem) (ible:suffix)) ity:suffix))
accretion (S (accrete:stem) (ion:suffix))
activist (S (active:stem) ist:suffix))
adaptable (S (adapt:stem) able:suffix))
addiction (S (addict:stem) ion:suffix))
</code></pre>

<p>The awk code, I use is </p>

<pre><code>awk 'BEGIN{FS=OFS="\n"}{
   a=gensub(/([a-zA-Z]*):stem/,"( S\\1:stem)", "g");
   while ( a ~ /stem)&lt;&gt;.*:suffix/) {
     a=gensub(/(\([a-zA-Z]*:stem\).*?)&lt;&gt;([a-zA-Z]*):suffix/,"(\\1\\2:suffix)", "g", a);
   }
   while ( a ~ /&lt;&gt;/) {
     a=gensub(/([a-zA-Z]*?:prefix)&lt;&gt;(.*)/,"(S\\1\\2)", "g", a);
   }
   print a;}
</code></pre>

<p>This code fails to produce desired output and produce results for only 5 tokens.</p>