#!/usr/bin/perl use warnings; use strict; # firstly, put all data in hashes because it's easier to look for data in a hash my @input = ({},{}); # 2 hash references my @output; while (<>) { # this means: and/or filenames from @ARGV if (/^>([^\s]+)(\s+)?([^\s]+)?$/) { ++$input[0]->{$1}; # first match is an element of 1st column defined $3 && ++$input[1]->{$3}; # third match might not exist and belongs to 2nd column } } # secondly, loop over first hash searching for elements in second hash for (keys %{$input[0]}) { unless (exists ${$input[1]}{$_}) { push @output,$_; } } print join "\n",@output;