Eu usei perl aqui, pois tem estruturas de dados mais flexíveis (hash de hashes de arrays).
use strict;
use warnings;
use v5.10;
use autodie;
my (%f1, @order);
open my $fh, "<", "f1.csv";
while (<$fh>) {
next if $. == 1;
chomp;
my ($p, $v, $ts) = split /,/;
push @{ $f1{$p}{$v} }, $ts;
push @order, [$p, $v];
}
close $fh;
my %f2;
open $fh, "<", "f2.csv";
while (<$fh>) {
print if $. == 1;
chomp;
my ($p, $v, $rts, $uts) = split /,/;
$f2{$p}{$v} = [$rts, $uts];
}
close $fh;
for my $key (@order) {
my ($p, $v) = @$key;
if (exists $f2{$p}{$v}) {
my $uts = shift @{$f1{$p}{$v}}
say join(",", $p, $v, $f2{$p}{$v}[0], $uts);
}
}
my @remaining;
for my $p (keys %f1) {
for my $v (keys %{$f1{$p}}) {
my ($rts, $uts) = @{$f1{$p}{$v}};
push @remaining, [$p, $v, $rts, $rts] if $rts;
push @remaining, [$p, $v, $rts, $uts] if $rts and $uts;
}
}
say for map {join ",", @{$_->[1]}}
sort {$a->[0] cmp $b->[0]}
map {[$_->[3], $_]}
@remaining;
A execução produz:
P,V,RTS,UTS
p1,12,10:00:00,10:10:00
p2,34,10:07:00,10:21:00
p1,12,10:00:00,10:21:00
p2,34,10:07:00,10:22:00
p3,60,10:25:00,10:36:00
p4,22,10:30:00,10:38:00
p1,60,10:35:00,10:35:00
p4,42,10:40:00,10:40:00
p1,60,10:35:00,10:41:00
p3,58,10:42:00,10:42:00
p2,55,10:45:00,10:45:00
p3,58,10:42:00,10:55:00