É um pouco mais complicado do que parece - provavelmente há uma biblioteca que faria isso melhor (há muitas bibliotecas de matemática no perl).
Mas isso deve fazer aproximadamente o que você deseja:
#!/usr/bin/perl
use strict;
use warnings;
#read file 1
open( my $file1, "<", "data1.txt" ) or die $!;
my $header_line = <$file1>;
chomp($header_line);
my ( $column1, @headers1 ) = split( ' ', $header_line );
my %results;
my %headers_in_file1 = map { $_ => 1 } @headers1;
for (<$file1>) {
my ( $column, @values ) = split;
my %these_results;
@these_results{@headers1} = @values;
$results{$column} = \%these_results;
}
close ( $file1);
#read file 2
open( my $file2, "<", "data2.txt" ) or die $!;
$header_line = <$file2>;
chomp($header_line);
my ( $column2, @headers2 ) = split( ' ', $header_line );
my %results2;
my %headers_in_file2 = map { $_ => 1 } @headers2;
for (<$file2>) {
my ( $column, @values ) = split;
my %these_results;
@these_results{@headers2} = @values;
$results2{$column} = \%these_results;
}
close ( $file2 );
#figure out the columns in both
my %in_both;
foreach my $header ( @headers1, @headers2 ) {
if ( $headers_in_file1{$header}
and $headers_in_file2{$header} )
{
$in_both{$header}++;
}
}
#sort out headers for output.
my @output_headers = sort keys %in_both;
print join( " ", $column1, @output_headers ), "\n";
foreach my $row ( sort keys %results ) {
print $row, " ";
for my $header (@output_headers) {
print $results{$row}{$header}, " ";
}
print "\n";
}
print "Second\n";
print join( " ", $column2, @output_headers ), "\n";
foreach my $row ( sort keys %results2 ) {
print $row, " ";
for my $header (@output_headers) {
print $results2{$row}{$header}, " ";
}
print "\n";
}