title: Looking for a way to have awk iteratively loop through a file (to create PERCENTRANK function in bash)
body: <p>My file <strong>data.txt</strong> contains the following record structure with millions of rows:</p>
  
  <pre><code>13
  12
  11
  8
  4
  3
  2
  1
  1
  1
  </code></pre>
  
  <p>For every value of this column, I need to calculate its PERCENTRANK (which is the rank of a value in a data set as a percentage of the data set).</p>
  
  <p>The formula to calculate the PERCENTRANK of any value X in the data set is</p>
  
  <pre><code>= number of values less than X / (Number of values less than X + Number of Values greater than X)
  </code></pre>
  
  <p>So, for every value X in the data set, the program has to loop through all the records to find the number of values less than and greater than X in the same data set.</p>
  
  <p>How can this be implemented using 'awk' to repeatedly loop through the file to calculate PERCENTRANK for all values of X?</p>
  
  <p>Expected output:</p>
  
  <pre><code>X    PERCENTRANK
  13   1.0000
  12   0.8888
  11   0.7777
  8    0.6666
  4    0.5555
  3    0.4444
  2    0.3333
  1    0.0000
  1    0.0000
  1    0.0000
  </code></pre>
  
  <p>PERCENTRANK of 2 is 0.333 because three values in the set are less than 2, and six are greater than 2. PERCENTRANK OF 2 = 3 / (3 + 6) = 3/9 = 0.3333.</p>
  
  <p>Similarly, PERCENTRANK of 4 is 0.5555 because five values are less than 4, and four are greater. PERCENTRANK OF 4 = 5 / (5 + 4) = 5 / 9 = 0.5555.</p>
  
  <p>I am avoiding a nested 'while..do' loop because it is terribly slow when looping through a file containing millions of records.</p>
  
  <p>I have been pleased with the amazing speed of awk in many other iterative computing scenarios, for example: calculating averages, standard deviations, group by sum, etc. and so, I would ideally like to use 'awk' to solve this use case as well.</p>