How to match 2 columns where one column has data as a range


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to match 2 columns where one column has data as a range
# 1  
Old 04-01-2010
Bug How to match 2 columns where one column has data as a range

Hi,

I have a query about joining files using data ranges.
Example files below - I want to join file1 to file2 with matches where file1 column 1 is equal to file2 column1, and file1 column 2 is within the range of file2 columns 3 and 4. I would like rows which don't match to be printed too.

Thanks!!


file1
A 1
A 2
A 3
B 1
B 2
B 3
B 4
C 1
C 2
C 3
C 4

file 2
A Exon 2 3
B Exon 1 2
C Exon 3 4

output file required
A 1 -
A 2 Exon
A 3 Exon
B 1 Exon
B 2 Exon
B 3 -
B 4 -
C 1 -
C 2 -
C 3 Exon
C 4 Exon
# 2  
Old 04-01-2010
Try this:
Code:
awk 'NR==FNR{a[$1]=$2;b[$1]=$3;c[$1]=$4;next}
a[$1] && $2 >= b[$1] && $2 <= c[$1]{print $0 FS a[$1];next}
{print $0 FS " -"}' file2 file1

# 3  
Old 04-01-2010
Beautiful! Works perfectly.
If you have time, would it be possible to post a few notes on what each line does? Just for learning...
Thank you!Smilie
# 4  
Old 04-03-2010
Quote:
Originally Posted by auburn
Beautiful! Works perfectly.
If you have time, would it be possible to post a few notes on what each line does? Just for learning...
Thank you!Smilie
Sure:

Code:
awk 'NR==FNR{a[$1]=$2;b[$1]=$3;c[$1]=$4;next}
a[$1] && $2 >= b[$1] && $2 <= c[$1]{print $0 FS a[$1];next}
{print $0 FS " -"}' file2 file1

Explanation:

Code:
NR==FNR

When we read the first file (file2)

Code:
{a[$1]=$2;b[$1]=$3;c[$1]=$4;next}

Fill the associative arrays a, b and c with the first field as index and get the next line of file2

Now:

a[$1] contains the 2nd field of file2
b[$1] contains the 3th field of file2
c[$1] contains the 4th field of file2

The code of the next line processes file1:

Code:
a[$1] && $2 >= b[$1] && $2 <= c[$1]{print $0 FS a[$1];next}

Code:
If the 1st field matches with an index of array a 
   and the 2nd column >= the 3th field of file2 
   and the 2nd column >= the 4th field of file2
then
   print the current row of file1, a fieldseparator, the 2nd field of file2 
   and get the next record of file1

Code:
{print $0 FS " -"}'

If the 1st field don't match with an index of array a print the line, a fieldseparator and " -"
# 5  
Old 04-05-2010
Thank you, this is very helpful!

Cheers Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. Shell Programming and Scripting

Match first two columns and calculate percent of average in third column

I have the need to match the first two columns and when they match, calculate the percent of average for the third columns. The following awk script does not give me the expected results. awk 'NR==FNR {T=$3; next} $1,$2 in T {P=T/$3*100; printf "%s %s %.0f\n", $1, $2, (P>=0)?P:-P}' diff.file... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

3. Shell Programming and Scripting

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

4. Shell Programming and Scripting

Match words and fetch data in front of it in second column

Hi all, I have 2 files one file contain data like this in one column AST3 GSTY4 JST3 second file containign data like this in 2 columns AST3(PAXXX),GSTY4(PAXXY) it is used in diabetes KST4 it is used in blood... (6 Replies)
Discussion started by: manigrover
6 Replies

5. Shell Programming and Scripting

Match same file column data

File A B07 U51C 4434 L662C 4412 B07 L64U 612 L651B 4434 B07 L11C 4434 R151B 4434 B05 L12Z 612 L51B 4434 B01 651Z 612 L651C 4434 B04 A51Z 612 L51A 4434 L07 B08D 4434 B1B 4434 B07 RU8D 4434 L51A 4434 B07 L58D 4434 B51C 4434 B07 LA8D 4434 L4B 4434 Now i want File B Output B07... (2 Replies)
Discussion started by: asavaliya
2 Replies

6. UNIX for Dummies Questions & Answers

How to match 2 columns where one column has data as a range - extended

Dear all, there is a nice solution for a text merge where the second file has only variables with a numeric range ( sorry, cannot post URL + thread is closed ). The real world is however more complicated than in the earlier example. file1 A 1 A 2 A 3 B 1 B 2 B 3 B 4 C 1 C 2 C 3 C... (4 Replies)
Discussion started by: underscore
4 Replies

7. Shell Programming and Scripting

awk to match a numeric range specified by two columns

Hi Everyone, Here's a snippet of my data: File 1 = testRef2: A1BG - 13208 13284 AAA1 - 34758475 34873943 AAAS - 53701240 53715412File 2 = 42MLN.3.bedS2: 13208 13208 13360 13363 13484 13518 13518My awk script: awk 'NR == FNR{a=$1;next} {$1>=a}{$1<=a}{print... (5 Replies)
Discussion started by: heecha
5 Replies

8. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

9. UNIX for Dummies Questions & Answers

two files.say a and b.both have long columns.i wanna match the column fron 1st file w

ex: a file has : 122323 123456456 125656879 678989965t635 234323432 b has : this is finance no. this is phone no this is extn ajkdgag idjsidj i want the o/p as: 122323 his is finance no. 123456456 this is phone no 123456456 ... (4 Replies)
Discussion started by: TRUPTI
4 Replies

10. Shell Programming and Scripting

Convert two column data into 8 columns

Apologies if this has been covered - I did search but couldn't find what I was looking for. I have a simple X-Y input file. I want to convert it from two columns into 8 columns - 4 pairs of X-Y data. So my input file looks like X1 Y1 X2 Y2 X3 Y3 X4 Y4 X5 Y5 etc And I want it to look... (8 Replies)
Discussion started by: NickC
8 Replies
Login or Register to Ask a Question