Relational Join (Composite Key)


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Relational Join (Composite Key)
# 1  
Old 06-08-2009
Question Relational Join (Composite Key)

hi, i have
file 1:
======
0501000|X1
0502000|X2
0501231|X3
0981222|X4
0502000|X6
0503000|X7
0932322|X8

file 2:
=======
050
0501
0502
09
098


the required output is .. to find for each record, in (ORDER by LENGTH) to join
file 1 to file 2
: as example:

0501000|X1 ===> is joined to ===> 0501* & not 050
0502000|X2 ===> is joined to ===> 0502* & not 050
but
0503000|X7 ===> is joined to ===> 050*

this can be true to any length of file2: for example: it could be 050XX000 ==> 050XX0 & not (050X*, 050XX*)


can anybody help .. giving ideas using sort /join

note: file1: is 10.5 Million line or record
file2 : is 1000 record

Advice really appreaciated with performance kept in mind.
Thanks
# 2  
Old 08-05-2009
If performance is a consideration then perl is probably not a good approach. If your data is in a database table, which you are already alluding to, then you need a smart use of the LIKE operator as well as wildcards ('%', '_' etc). Details of what is possible within MySQL can be found here: MySQL :: MySQL 5.0 Reference Manual :: 11.4.1 String Comparison Functions
Let me know what you ultimately will use, because I find the problem quite interesting.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join and merge multiple files with duplicate key and fill void columns

Join and merge multiple files with duplicate key and fill void columns Hi guys, I have many files that I want to merge: file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: (5 Replies)
Discussion started by: yjacknewton
5 Replies

2. Shell Programming and Scripting

Join 2 CSVs based on 1 key

Hello, I have 2 csv as follows: a.csv: name,phone,adress,car xy,1234,asbd yz,2134,asbdf tc,6789,salkdur b.csv: telphone,vehicle 2134,toyota 6789,bmw 1234,honda What is need is this: output.csv: name,phone,adres,car xy,1234,asbd,honda yz,2134,asbdf,toyota (7 Replies)
Discussion started by: Zam_1234
7 Replies

3. Shell Programming and Scripting

Linux - Join 2 csv files with common key

Hi, I am trying to join 2 csv files, to create a 3rd output file with the joined data. Below is an example of my Input Data: Input File 1 NAME, FAV_FOOD, FAV_DRINK, ID, GENDER Bob, Fish, Coke, 1, M Lisa, Rice, Water, 2, F Jenny, Noodle, Tea, 3, F Ken, Pizza, Coffee, 4, M Lisa,... (7 Replies)
Discussion started by: RichZR
7 Replies

4. Shell Programming and Scripting

Generate Join clause based on key data

Hi, I have a file pk.txt which has pk data in following format TableName | PK Employee | id Contact|name,country My Output should be Employee | t1.id=s.id Contact| t1.name=s.name AND t1.country=s.country I started of like this: for LIST in `cat pk.txt` do... (5 Replies)
Discussion started by: wahi80
5 Replies

5. Shell Programming and Scripting

Making a composite file of transposed columns

Hello, I have a directory with allot of tab delimited text files that have data that look like, filePath distance (1,4-dioxan-2-ylmethyl)methylamine 0.0 4-methylmorpholine 0.0755473632594 1-propyl-4-piperidone 0.157792911954 heptaminol 0.158142893249 N-acetylputrescine 0.158689628956... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

6. Shell Programming and Scripting

Left Join in Unix based on Key?

So I have 2 files: File 1: 111,Mike,Stipe 222,Peter,Buck 333,Mike,Mills File 2: 222,Mr,Bono 444,Mr,Edge I want output to be below, where 222 records joined and all none joined records still in output 111,Mike,Stipe 222,Peter,Buck,Mr,Bono 333,Mike,Mills 444,Mr,Edge (4 Replies)
Discussion started by: stack
4 Replies

7. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

8. Shell Programming and Scripting

Join 3 files using key column in a mapping file

I'm new of UNIX shell scripting. I'm recently generating a excel report in UNIX(file with delimiter is fine). How should I make a script to do it? 1 file to join comes from output of one UNIX command, the second from another UNIX command, and third from a database query. The key columes of all... (7 Replies)
Discussion started by: bigsmile
7 Replies
Login or Register to Ask a Question