This perl version expects input on stdin or from filenames(s) specified on command line, and prints results to stdout.
It uses a hash to keep track of how many times it has seen the same values together (ignoring subsequent lines unless they are in reverse order from the first). Twice means the relationship between column A and column B is bidirectional A<->B, while once means uni-directional from A->B.
Hashes are inherently unordered, so we can use the following array to preserve the order of input lines, and print the results in a similar order (won't be exactly the same because we only print AB even if BA was also seen)
If the order of the output is not important, then comment out or delete the my @order line and the push line, and swap the comment char on the two versions of the foreach lines below.
#! /usr/bin/perl
use strict;
# hash keys will be 'columnA-columnB', we'll split them when we print the report.
my %AB=();
my @order=();
while(<>) {
chomp;
my ($A, $B) = split;
# have we seen a key B-A? if so, increment that rather than define A-B
if (defined($AB{$B . '-' . $A})) {
$AB{$B . '-' . $A} = 2;
} elsif (! defined($AB{$A . '-' . $B})) {
$AB{$A . '-' . $B} = 1;
push @order, $A . '-' . $B;
}
}
#foreach my $key (sort keys %AB) {
foreach my $key (@order) {
my ($A,$B) = split /-/,$key;
if ($AB{$key} == 1) {
print "$A -> $B\n";
} elsif ($AB{$key} == 2) {
print "$A <-> $B\n";
} ;
}
Output:
Mike <-> John
Pamela <-> Barbara
Mike -> Paul
Roger -> Paul