open ( FH, "p.txt" ); my @output = ; close ( FH ); #find the line which has "myFiles" in it my @line = grep ( /myFiles/ ), @output; #hopefully I should only one line back my $i = $line[0]; #will this work? $i =~ s/.*(test.*\.txt).*/test29-01-05\.txt/