I agree with
hawtin about making a hash with keys based on the source and destination (reversed for replies). But what you need to gather the messages together is a hash of arrays. That will also preserve the sequence within each exchange, since I assume the log is in time order. (It will not preserve the overall sequence, but that could be another sort after you have them grouped).
Here is my implementation:
use strict;
my %calls;
while (<>) {
my $line = $_;
my ($num, $source, $dest, $type) = split (/\s+/,$line,5);
my $key;
if ($type eq "GetReply") {
$key = $dest . ":" . $source;
} else {
$key = $source . ":" . $dest;
}
push @{$calls{$key}}, $line;
}
foreach my $k (sort keys %calls) {
print join('', @{$calls{$k}}),"\n\n";
}