Code:
skrynesaver@busybox ~/tmp$ cat tmp.dat tmp_2.dat
Date_Time Context D1 D2
04/19/2013_23:48:54.819 ABCD x x
04/19/2013_23:48:55.307 ABCD x x
04/19/2013_23:48:55.823 ABCD x x
04/19/2013_23:48:56.309 ABCD x x
Date_Time Context D3 D4
04/19/2013_23:48:54.819 ABCD y y
04/19/2013_23:48:55.821 ABCD y y
04/19/2013_23:48:56.799 ABCD y y
04/19/2013_23:48:57.819 ABCD y y
skrynesaver@busybox ~/tmp$ perl -Mstrict -e '
my %record;
open (DAT_1 , "<", $ARGV[0]);
while(<DAT_1>){
chomp;
if (/^\d/){
my @record=split/\s+/,$_;
$record{reorder($record[0])}=join(",",@record," "," ");
}
}
close(DAT_1);
open(DAT_2,"<",$ARGV[1]);
while(<DAT_2>){
chomp;
if(/^\d/){
my @record=split/\s+/,$_;
$record{reorder($record[0])}=$record{reorder($record[0])}
?sublast($record{reorder($record[0])},@record[2,3])
:join(",",@record[0,1]," "," ",@record[2,3]);
}
}
END{
for my $timestamp (sort keys %record){
print $record{$timestamp},"\n";
}
}
sub reorder{
my $date=shift;
my @date=split/\/|_/,$date;
return join("_",@date[2,1,0,3]);
}
sub sublast{
my ($record,@fields)=@_;
my @record=split/,/,$record;
@record[4,5]=@fields;
return join",",@record
}' tmp.dat tmp_2.dat
04/19/2013_23:48:54.819,ABCD,x,x,y,y
04/19/2013_23:48:55.307,ABCD,x,x, ,
04/19/2013_23:48:55.821,ABCD, , ,y,y
04/19/2013_23:48:55.823,ABCD,x,x, ,
04/19/2013_23:48:56.309,ABCD,x,x, ,
04/19/2013_23:48:56.799,ABCD, , ,y,y
04/19/2013_23:48:57.819,ABCD, , ,y,y