title: how do I use awk to edit a file
body: <p>I have a text file like this small example:</p>
  
  <pre><code>chr10:103909786-103910082   147 148 24  BA
  chr10:103909786-103910082   149 150 11  BA
  chr10:103909786-103910082   150 151 2   BA
  chr10:103909786-103910082   152 153 1   BA
  chr10:103909786-103910082   274 275 5   CA
  chr10:103909786-103910082   288 289 15  CA
  chr10:103909786-103910082   294 295 4   CA
  chr10:103909786-103910082   295 296 15  CA
  chr10:104573088-104576021   2925    2926    134 CA
  chr10:104573088-104576021   2926    2927    10  CA
  chr10:104573088-104576021   2932    2933    2   CA
  chr10:104573088-104576021   58  59  1   BA
  chr10:104573088-104576021   689 690 12  BA
  chr10:104573088-104576021   819 820 33  BA
  </code></pre>
  
  <p>in this file there are 5 <code>tab separated</code> columns. the first column is considered as ID. for example in the first row the whole "<code>chr10:103909786-103910082</code>" is ID.</p>
  
  <p><code>1-</code> in the 1st step I would like to filter out the rows based on the 4th column. 
  if the number in the <code>4th column is less than 10</code> and the same row but in the <code>5th column the group is BA</code>, that row will be filtered out. also if the number in the <code>4th column is less than 5</code> and the same row but in the <code>5th column the group is CA</code>, that row will be filtered out.</p>
  
  <p><code>3-</code> 3rd step:
  I want to get the ratio of number in <code>4th column</code>. in fact in the <code>1st column</code> there are repeated values which represent the same <code>ID</code>. I want to get one ratio per <code>ID</code>, so in the output every <code>ID</code> will be repeated only once. each <code>ID</code> has both <code>BA</code> and <code>CA</code> in the <code>5th column</code>. for each <code>ID</code> I should get 2 values for <code>CA</code> and <code>BA</code> separately and get the ration of <code>CA/BA</code> as the final value for each <code>ID</code>. to get one value as <code>CA</code>, I should add up all values in the <code>4th column</code> which belong the same <code>ID</code> and classified as <code>CA</code> and to get one value as <code>BA</code>, I should add up all values in the <code>4th column</code> which belong the same <code>ID</code> and classified as <code>BA</code>. the last step is to get the ration of <code>CA/BA</code> per <code>ID</code>. the expected output for the small example would look like this:</p>
  
  <p><code>1- after filtration</code>:</p>
  
  <pre><code>chr10:103909786-103910082   147 148 24  BA
  chr10:103909786-103910082   149 150 11  BA
  chr10:103909786-103910082   274 275 5   CA
  chr10:103909786-103910082   288 289 15  CA
  chr10:103909786-103910082   295 296 15  CA
  chr10:104573088-104576021   2925    2926    134 CA
  chr10:104573088-104576021   2926    2927    10  CA
  chr10:104573088-104576021   689 690 12  BA
  chr10:104573088-104576021   819 820 33  BA
  </code></pre>
  
  <p><code>2- after summarizing each group</code> (<code>CA</code> and <code>BA</code>):</p>
  
  <pre><code>chr10:103909786-103910082   147 148 35  BA
  chr10:103909786-103910082   274 275 35  CA
  chr10:104573088-104576021   2925    2926    144 CA
  chr10:104573088-104576021   819 820 45  BA
  </code></pre>
  
  <p>3- the <code>final output</code>(this ratio is made using the values in 4th column):</p>
  
  <pre><code>chr10:103909786-103910082   1
  chr10:104573088-104576021   3.2
  </code></pre>
  
  <p>in the above lines, 1 = 35/35 and 3.2 = 144/45.</p>
  
  <p>I am trying to do that in <code>awk</code></p>
  
  <pre><code>awk 'ID==$1 {
           if (ID) {
               print ID, a["CA"]/a["BA"]; a["CA"]=a["BA"]=0;
           }
           ID=$1
       }
       $5=="BA" &amp;&amp; $4&gt;=10 || $5=="CA" &amp;&amp; $4&gt;=5 { a[$5]+=$4 }
       END{ print ID, a["CA"]/a["BA"] }' file.txt
  </code></pre>
  
  <p>I tried to use the code but did not succeed. this code returns one number. in fact sum of all CA and divides it by sum of all <code>BAs</code> but I want to do that per ID and get the ration per ID. do you know how to solve the problem and correct the code?</p>