#!/usr/bin/perl -w use strict; use Text::CSV_XS; open (my $FILE1, '<', "file1.csv") or die "cannot open file1 $!\n"; open (my $FILE2, '<', "file2.csv") or die "cannot open file3 $!\n"; open (my $FILE3, '>', "file3.csv") or die "cannot open file3 $!\n"; my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/ , sep_char => "\t", always_quote =>1}); print $FILE3 "Match\t".<$FILE1>; # header for file3 <$FILE2>; # skip header line of file2 my %file1; my %file2; while (my $row = $csv->getline($FILE1)) { my @fields = @$row; my $id = $fields[4]; $file1{$id}=["",@fields]; } while (my $row = $csv->getline($FILE2)) { my @fields = @$row; my $id = $fields[0]; $file2{$id}=["","","","","",@fields,"","","","",""]; } foreach my $id1 (keys %file1) { if (exists $file2{$id1}) { $file1{$id1}[0] ="both"; #both files $csv->print ($FILE3, $file1{$id1}); } else { $file1{$id1}[0] ="1"; #file1 only $csv->print ($FILE3, $file1{$id1}); } } foreach my $id2 (keys %file2) { if (!exists $file1{$id2}) { $file2{$id2}[0] ="2"; #file2 only $csv->print ($FILE3, $file2{$id2}); } }