#!/usr/bin/perl use strict; use warnings; my %hash; my $infile="File1.csv"; open (my $in_fh, "<", $infile) or die ($!); my $line=<$in_fh>; chomp $line; my @columnheadings= split (/\t/,$line); while (my $line = <$in_fh>){ chomp $line; $line=~ s/\t/,/g; $line=~tr/[a-z]/[A-Z]/; my @columns=split (/,/,$line); my $Uniqueid=$columns[4]; my $newline; unshift(@columns, 'File1'); $newline=join(",",@columns); if (exists($hash{$Uniqueid})){ print "$hash{$Uniqueid}\n"; } $hash{$Uniqueid}=$newline; } my $discard=<$in_fh>; $infile="File2.csv"; open ($in_fh, "<", $infile) or die ($!); while (my $line= <$in_fh>){ chomp $line; $line=~s/\t/,/g; $line=~tr/[a-z]/[A-Z]/; my @columns= split (/,/,$line); my $Uniqueid=$columns[0]; my $newline; if (exists($hash{$Uniqueid})){ $line=~ s/^File1,/both,/; $newline=$line; } else { $newline="File2,,,,".$line; } $hash{$Uniqueid}=$newline; } $discard=<$in_fh>; my $outfile="OutputFile.csv"; open (my $out_fh, ">", $outfile) or die ($!); unshift (@columnheadings, ('Match')); my $headings=join (",", @columnheadings); print $out_fh "$headings\n"; use Data::Dumper; print Dumper \@columnheadings; foreach my $id (sort keys (%hash)) { my @columns=split(/,/,$hash{$id}); my $printline=$hash{$id}; while ($printline=~s/,,/,NA,/g) { }; print $out_fh "$printline\n"; delete $hash{$id}; }