matching columns with overlapping value ranges

Hi,

I want to match and print columns that match.

So my file looks like this:

h1 20 30 h1 25 27
h2 50 70 h2 90 95
h2 60 80 h2 70 75
h3 130 150 h3 177 190
h4 140 190 h4 300 305

So there are 6 columns. Column 1 and 4 are names. I am able to get the names to match up into rows. But what I want to do next is match columns 2,3,5 and 6. So if columns 2-3 overlap wiht 5-6 then I want to print that line (eg. values 20-30 overlap with 25-27 so they print. But on row 5, 140-190 does not overlap with 300-305 so it wont print).

So the output will look like this:
h1 20 30 h1 25 27
h2 60 80 h2 70 75
h4 140 190 h4 300 305

Basically I have this... it can match columns 1 and 4 but I cant get the other columns to match.

awk -F"\t" '$1==$4 {print $0} ' file1.txt > output.txt

thanks

Is this what you want?

awk -F"\t" '
 $1 == $4 {
 if ( $3 < $5 || $2 > $6 ) next
 print
}' file1.txt > output.txt
$
$ cat file1.txt
h1      20      30      h1      25      27
h2      50      70      h2      90      95
h2      60      80      h2      70      75
h3      130     150     h3      177     190
h4      140     190     h4      300     305
h5      200     225     h5      175     190
h6      300     350     h6      235     305
$
$ awk -F"\t" '$1==$4 && $5<=$3 && $6>=$2 {print $0}' file1.txt
h1      20      30      h1      25      27
h2      60      80      h2      70      75
h6      300     350     h6      235     305
$
$

tyler_durden