use strict; use warnings; my @sn; # store the line of file1.txt into array my $i=0; my $lctr=0; my $flag=1; while (<>) { # read the arg input file "file1.txt" that contains info push @sn, split ' '; # store each line to array print "sn[$i]=$sn[$i]\n"; $i++; } $sn[$i] = "END"; # mark the end of the array print $sn[$i]; my $wait=; my $filename = 'file2.txt'; open(my $fh, '<:encoding(UTF-8)', $filename) or die; $i=0; while ($flag==1) { while (my $row = <$fh>) { chomp $row; print "Searching for $sn[$i]...."; if (index($row, $sn[$i]) != -1) { print $row; print "Found $sn[$i]\n"; my $wait = ; $i++; seek FH, 0, 0; } $lctr++; if ($sn[$i] eq 'END') { $flag=0; last; } } }