title: Awk conditional filter one file based on another (or other solutions)
body: <p>Programming beginner here needs some help modifying an AWK script to make it conditional. Alternative non-awk solutions are also very welcome.</p>
  
  <p><strong>NOTE</strong> Main filtering is now working thanks to help from Birei but I have an additional problem, see note below in question for details.</p>
  
  <p>I have a series of input files with 3 columns like so: </p>
  
  <pre><code>chr4    190499999   190999999
  chr6    61999999    62499999
  chr1    145499999   145999999
  </code></pre>
  
  <p>I want to use these rows to filter another file (refGene.txt) and if a row in file one mathces a row in refGene.txt, to output column 13 in refGene.txt to a new file 'ListofGenes_$f'.
  The tricky part for me is that I want it to count as a match as long as column one (eg 'chr4', 'chr6', 'chr1' ) and column 2 AND/OR column 3 matches the equivalent columns in the refGene.txt file. The equivalent columns between the two files are $1=$3, $2=$5, $3=$6.
  Then I am not sure in awk how to not print the whole row from refGene.txt but only column 13. </p>
  
  <p><strong>NOTE</strong> I have achieved the conditional filtering described above thanks to help from Birei. Now I need to incorporate an additional filter condition. I also need to output column $13 from the refGene.txt file if any of the region between value $2 and $3 overlaps with the region between $5 and $6 in the refGene.txt file. This seems a lot trickier as it involves mathmatical computation to see if the regions overlap. </p>
  
  <p>My script so far:</p>
  
  <pre><code>FILES=/files/*txt   
  for f in $FILES ;
  do
  
      awk '
          BEGIN {
              FS = "\t";
          }
          FILENAME == ARGV[1] {
              pair[ $1, $2, $3 ] = 1;
              next;
          }
          {
              if ( pair[ $3, $5, $6 ] == 1 ) {
                  print $13;
              }
          }
      ' $(basename $f) /files/refGene.txt &gt; /files/results/$(basename $f) ;
  done
  </code></pre>
  
  <p>Any help is really appreciated. Thanks so much!</p>
  
  <p>Rubal</p>