title: Pair-wise trimming of files based of max/min values of first/last record
body: <p>I have a bit of problem with rather large csv files. I am able to write simple bash/awk scripts, but this problem is harder for my limited awk/bash programming experience.</p>
  
  <p><strong>Problem:</strong></p>
  
  <ul>
  <li><p>All my files are in folders. Folders have an even number of csv files that need trimming pair-wise (I will explain with this means). Files are named like: f1L, f1R, f2L, f2R, f3L, f3R, ..., fnL,fnR.</p></li>
  <li><p>Files need reading in pairs, ie. f1L with f1R. f2L with f2R and so on</p></li>
  <li><p>Files have two comma-separated fields. f1L (file start/end) and f1R, look like</p></li>
  </ul>
  
  <blockquote>
  <pre><code>f1L (START)
  1349971210, -0.984375 
  1349971211, -1.000000 
  
  f1R (START) 
  1349971206, -0.015625
  1349971207, 0.000000
  
  f1L (END)
  1350230398, 0.500000
  1350230399, 0.515625
  
  f1R (END) 
  1350230402, 0.484375
  1350230403, 0.515625
  </code></pre>
  </blockquote>
  
  <p>What I would like to do with awk is:</p>
  
  <ol>
  <li>Read record 1, field 1 of  f1L (i.e 1349971210) then  record 1, field 1 of f1R (i.e 1349971206). Then take the maximum of both values (i.e x1 = 1349971210). </li>
  <li>Read last record , field 1 of  f1L (i.e 1350230399) then  last record, field 1 of f1R (i.e 1350230403). Then take the minimum value (i.e x2 = 1350230399).</li>
  <li>Then extract and re-save with the same name all the lines in f1L and f1R between bigger than/equal to x1 and smaller than/equal x2.</li>
  <li>Repeat process for all the pairs in my directory. </li>
  </ol>
  
  <p>Wondered if any of you have any suggestions of a little script with bash/awk to get the job done. </p>