matching columns with overlapping value ranges


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting matching columns with overlapping value ranges
# 1  
Old 06-07-2009
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
# 2  
Old 06-07-2009

Is this what you want?

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


Last edited by cfajohnson; 06-07-2009 at 06:27 PM..
# 3  
Old 06-07-2009
Code:
$
$ 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching first 2 columns..

Hello All, I want to make a file which will have primarily lines of file2 but when first 2 fields matches with the file1 it should have those lines of file1.. example is as below.. file1 a;b;1 c;d f;e t;r;5 file2 b;g a;b c;d v;b f;e t;r (2 Replies)
Discussion started by: ailnilanjan
2 Replies

2. Shell Programming and Scripting

Identify the overlapping and non overlapping regions

file1 chr pos1 pos2 pos3 pos4 1)chr1 1000 2000 3000 4000 2)chr1 1380 1480 6800 7800 3)chr1 6700 7700 1200 2200 4)chr2 8500 9500 5670 6670 file2 chr pos1 pos2 pos3 pos4 1)chr2 8500 9500 5000 6000 2)chr1 6700 7700 1200 2200 3)chr1 1380 1480 6700 7700 4)chr1 1000 2000 4900 5900 I... (2 Replies)
Discussion started by: data_miner
2 Replies

3. Shell Programming and Scripting

Join two files with matching columns

Hi, I need to join two files together with one common value in a column. I think I can use awk or join or a combination but I can't quite get it. Basically my data looks like this, with the TICKER columns matching up in each file File1 TICKER,column 1, column, 2, column, 3, column 4 ... (6 Replies)
Discussion started by: unkleruckus
6 Replies

4. UNIX for Dummies Questions & Answers

Extracting rows from a text file based on the values of two columns (given ranges)

Hi, I have a tab delimited text file with multiple columns. The second and third columns include numbers that have not been sorted. I want to extract rows where the second column includes a value between -0.01 and 0.01 (including both numbers) and the first third column includes a value between... (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Shell Programming and Scripting

Checking for ranges based on 2 columns

Hi, I have a file with 6 columns. I want to check if column 1 and 2 fall between column 5 and 6 I want to call them as "TRUE_genes" if not then call them as "FALSE_genes". I can do it for checking one column but how to mention about two columns. file1 110371373... (1 Reply)
Discussion started by: Diya123
1 Replies

6. IP Networking

Test for overlapping IP ranges

Greetings folks, I have a rather lengthy list of banned IP ranges in iptables. Initially it was constructed as a rather ad-hoc affair, then later I discovered a site which had IP Block By Country lists, and imported several into iptables. If possible, I'd like to be able to verify if the list... (0 Replies)
Discussion started by: putter1900
0 Replies

7. Shell Programming and Scripting

extracting columns falling within specific ranges for multiple files

Hi, I need to create weekly files from daily records stored in individual monthly filenames from 1999-2010. my sample file structure is like the ones below: daily record stored per month: 199901.xyz, 199902.xyz, 199903.xyz, 199904.xyz ...199912.xyz records inside 199901.xyz (original data... (4 Replies)
Discussion started by: ida1215
4 Replies

8. UNIX for Dummies Questions & Answers

matching columns

Hello experts, I have this problem, I need to match values based on two files, this is what I have: file1 1.1 1.2 1.3 5.5 1.4 1.5 1.6 file2 1 a 2 B 3 C 4 D 5 z (7 Replies)
Discussion started by: Gery
7 Replies

9. UNIX for Dummies Questions & Answers

Matching corresponding columns in two different files

Hi to all, I have two separated files: FILE1 "V1" "V2" "V3" Mary James Nicole Robert Francisco Sophie Nancy Antony Matt Josephine Louise Rose Mark Simon Charles FILE2 "V1" "V2" "V3"... (2 Replies)
Discussion started by: eleonoral
2 Replies

10. Shell Programming and Scripting

matching columns from two files

Hey, I have two files that have exactly the same format. They are both tab-delimited and contain 12 columns. However the # of rows vary. What I want to do is match columns # 5,6 and 7 between the two files. If they do match exactly (based on numbers) then I want the whole row from file 2 to... (1 Reply)
Discussion started by: phil_heath
1 Replies
Login or Register to Ask a Question