"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 only one input file's unmatched records are written to an "unmatched" output file.

My requirement is to

  1. Combine (more than 2) files into a single output file based on a common field (first field) in every file.

  2. If a particular "key" does not exist in one of the files but does exist in the others, that file's fields must be written to the line as blank fields.

In other words, with input files:

 
file1.txt
A|1|2|3|4
B|4|5|6|7
C|2|3|4|5
 
file2.txt
A|5|6|7|8|9|0
B|2|3|4|5|6|7
 
file3.txt
A|2|4|6
C|1|3|5
 
Required output:
 
A|1|2|3|4|5|6|7|8|9|0|2|4|6
B|4|5|6|7|2|3|4|5|6|7|||
C|2|3|4|5|||||||1|3|5

Anyone have any ideas using Perl or unix scripting?

Regards,

Bennie.

Not elegant, but does the job.

join -o0,1.2,1.3,1.4,1.5,2.2,2.3,2.4,2.5,2.6,2.7 -a1 -a2 -t\| f1 f2 |
  join -o0,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.10,1.11,2.2,2.3,2.4 -a1 -a2 -t\| - f3