my @lines = split /\n/, $temp; foreach my $idx ( 0 .. $#lines ) { next unless $lines[ $idx ] =~ m/File/; $found = $lines[ $idx + 1 ]; print $found; last; }