# matching columns with overlapping value ranges

**URL:** <https://community.unix.com/t/matching-columns-with-overlapping-value-ranges/241972>\
**Category:** Shell Programming and Scripting\
**Created:** [June 7, 2009, 3:59pm UTC](https://community.unix.com/t/matching-columns-with-overlapping-value-ranges/241972 "2009-06-07T15:59:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kylle345](https://community.unix.com/letter_avatar/kylle345/32/5_5575768a8748004e209b776fc1b2916d.png) [@kylle345](https://community.unix.com/u/kylle345)\
**Post date:** [June 7, 2009, 3:59pm UTC](https://community.unix.com/t/matching-columns-with-overlapping-value-ranges/241972/1 "2009-06-07T15:59:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [June 7, 2009, 5:10pm UTC](https://community.unix.com/t/matching-columns-with-overlapping-value-ranges/241972/2 "2009-06-07T17:10:06Z")

</div>

Is this what you want?

```nohighlight
awk -F"\t" '
 $1 == $4 {
 if ( $3 < $5 || $2 > $6 ) next
 print
}' file1.txt > output.txt

```

---

<div class="post-metadata">

**Author:** ![durden\_tyler](https://community.unix.com/user_avatar/community.unix.com/durden_tyler/32/1450_2.png) [@durden\_tyler](https://community.unix.com/u/durden_tyler)\
**Post date:** [June 7, 2009, 5:16pm UTC](https://community.unix.com/t/matching-columns-with-overlapping-value-ranges/241972/3 "2009-06-07T17:16:52Z")

</div>

```nohighlight
$
$ 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
