perl -lne '%ln;BEGIN{open $f,shift;map{chomp;$ln{$_}++}<$f>}print qq($ARGV line\t$.\t[$_]) if exists $ln{$_};close ARGV if eof' dog.txt cat.txt other.txt cat.txt line 2 [pretty woman] other.txt line 1 [pretty woman]