@ACCTS = ("1234", "5678", "6959"); open(IN,"InFile.txt"); %acct=""; while (defined($line = ())){ foreach $x (@ACCTS) { if ($line =~/$x/){ #only print the first line we see for each account unless( exists $acct{$x} ){ print "$line\n"; $acct{$x}++; } } } }