use strict;
# change these to change your input and output files
my $file_1 = 'File1.dat';
my $file_2 = 'File2.dat';
my $out_file = 'Output.dat';
my %headers;
my ($current_header1,$current_header2) = (0,0);
open(INPUT, $file_1)||die "Cannot open $file_1 for read:$!\n";
foreach(){
# match headers - we'll be a little fussy to avoid false positives
if(/^((\|(\d+\.){5}\d+){2}(\|.+){2})\|\d+\|\d+\s*$/){
$current_header1 = $1;
$current_header2 = 0;
}
elsif(/^((\|\d+){2})\s*$/ and $current_header1){
$current_header2 = $1;
}
elsif(/^((\|\d+){2})\|(\d+)\s*$/ and $current_header1 and $current_header2){
$headers{$current_header1}{$current_header2}{$1} = $2;
}
}
close INPUT||die "WTF? Couldn't close $file_1:$!\n";
$current_header1 = 0;
$current_header2 = 0;
open(INPUT, $file_2)||die "Cannot open $file_2 for read:$!\n";
foreach(){
# match headers - we'll be a little fussy to avoid false positives
if(/^((\|(\d+\.){5}\d+){2}(\|.+){2})\|\d+\|\d+\s*$/){
$current_header1 = $1;
$current_header2 = 0;
}
elsif(/^((\|\d+){2})\s*$/ and $current_header1){
$current_header2 = $1;
}
elsif(/^((\|\d+){2})\|(\d+)\s*$/ and $current_header1 and $current_header2){
$headers{$current_header1}{$current_header2}{$1} += $2;
}
}
# output the results...
open(OUT, ">$out_file")||die "Cannot open $out_file for write:$!\n";
foreach my $hkey(keys %headers){
print OUT "Header $hkey\n";
foreach my $subkey(keys %{$headers{$hkey}}){
print OUT "Subheader $subkey\n";
foreach my $objkey(keys %{$headers{$hkey}{$subkey}}){
print OUT "Object $objkey total:" . ${headers{$hkey}{$subkey}{$objkey}} . "\n";
}
}
}