#!/usr/bin/perl open(FH1,file1.txt); open(FH2,file2.txt); @array=; while($var=){ ($first,$second)=split("\t",$var); @grepped= grep ("$second",@array); print "$first\t@grepped\n"; }