title: awk to extract and print first occurrence of patterns
body: <p>I am trying to use <code>awk</code> to extract and print the first ocurrence of <code>NM_</code> and the portion after the<code>NP_</code> starting with <code>p.</code>. A <code>:</code> is printed instead of the "|" for each. The input file is <code>tab-delimeted</code>, but the output does not need to be. The below does execute but prints all the lines in the file not just the patterns. There maybe multiple <code>NM</code> or <code>NP</code> in my actual data of over 5000 lines, however only the first occurence of each is extracted and printed. I am still a little unclear on the <code>RSTART</code> and <code>RLENGHTH</code> concepts but, using line 1 as an example from the input:</p>
  
  <p>The <code>NM</code> variable would be <code>NM_020469.2</code></p>
  
  <p>The <code>NP</code> variable would be <code>:p.Gly268Arg</code></p>
  
  <p>I have included comments as well. Thank you :).</p>
  
  <p>input</p>
  
  <pre><code>Input Variant   HGVS description(s) Errors and warnings
  rs41302905  NC_000009.11:g.136131316C&gt;T|NM_020469.2:c.802G&gt;A|NP_065202.2:p.Gly268Arg
  rs8176745   NC_000009.11:g.136131347G&gt;A|NM_020469.2:c.771C&gt;T|NP_065202.2:p.Pro257=
  </code></pre>
  
  <p>desired output</p>
  
  <pre><code>rs41302905 NM_020469.2:c.802G&gt;A:p.Gly268Arg
  rs8176745 NM_020469.2:c.771C&gt;T:p.Pro257=
  </code></pre>
  
  <p>awk</p>
  
  <pre><code>awk -F'[\t|]' 'NR&gt;1{ # define FS as tab and `|` to split each, and skip header line
                r=$1; nm=np="";  # create variable r with $1 and 2 variables (one for nm and the other for np, setting them to null)
                for(i=2;i&lt;=NF;i++) { # start a loop from line2 and itterate
                    if ($i~/^NM_/) nm=$i;  # extract first NM_ in line and read into i
                    else if ($i~/^NP_/) np=substr($i,index($i,":")); # extract NP_ and print portion after : (including :)
                    if (nm &amp;&amp; np) { print r,nm np; break }  # print desired output
                }
            }' input
  </code></pre>