title: bash, find nearest next value, forward and backward
body: <p>I have a data.txt file</p>
  
  <pre><code>1    2     3    4      5       6        7   
  cat data.txt
  13 245 1323 10.1111 10.2222 60.1111 60.22222
  13 133 2325 11.2222 11.333  61.2222 61.3333
  13 245 1323 12.3333 12.4444 62.3333 62.44444444
  13 245 1323 13.4444 13.5555 63.4444 63.5555
  </code></pre>
  
  <p>Find next nearest: My target value is <code>11.6667</code> and it should find the nearest next value in <code>column 4</code> as <code>12.3333</code></p>
  
  <p>Find previous nearest: My target value is <code>62.9997</code> and it should find the nearest previous value in <code>column 6</code> as <code>62.3333</code></p>
  
  <p>I am able to find the next nearest (case 1) by</p>
  
  <pre><code>awk -v c=4 -v t=11.6667 '{a[NR]=$c}END{
          asort(a);d=a[NR]-t;d=d&lt;0?-d:d;v = a[NR]
          for(i=NR-1;i&gt;=1;i--){
                  m=a[i]-t;m=m&lt;0?-m:m
                  if(m&lt;d){
                      d=m;v=a[i]
                  }
          }
          print v
  }' f
  12.3333
  </code></pre>
  
  <p>Any bash solution? for finding the previous nearest (case 2)?</p>