merge two two txt files into one file based on one column

Hi,

I have file1.txt and file2.txt and would like to create file3.txt based on one column in UNIX

Eg:

file1.txt

17328756,0000786623.pdf,0000786623
20115537,0000793892.pdf,0000793892

file2.txt

12521_74_4.zip,0000786623.pdf
12521_15_5.zip,0000793892.pdf

Desired Output

file3.txt

17328756,0000786623.pdf,0000786623,12521_74_4.zip
20115537,0000793892.pdf,0000793892,12521_15_5.zip

Please advice!!

#!/usr/bin/perl
use Tie::File;
use strict;
use warnings;

my $output;
tie my @file1, "Tie::File", "file1.txt";
tie my @file2, "Tie::File", "file2.txt";

foreach my $line1 (@file1) {
        foreach my $line2 (@file2) {
        $output = join ",", $line1,$line2;
        }
open (FILE, ">>", 'file3.txt') or die "Cannot open file!\n";
print FILE "$output\n";
close FILE;
        }

Thanks for your time and reply. I am looking in Unix.

If you don't like perl try awk :rolleyes:

awk -F, 'NR==FNR{_[$2]=$1;next}{$0=_[$2]?$0FS _[$2]:$0}1' file2 file1

working fine..thx :)-

Sweet. What part of this command would I need to modify if I had 3 or more files that I want to output this way?

I actually have 5. file1 file2 file3 file4 file5

thanks.