$keyword = "your keyword here"; open hanw,"<" , "output.txt" or die "could not open output file $!"; opendir(hand,$dirpath); #replace with your DIR @files = readdir(hand); closedir(hand); foreach(@files){ if(/\.log$/i) { + #if the filename has .log at the end push(@logfiles,$_); } } $nooffiles = @logfiles; $fileindex=0; while ($fileindex <$nooffiles ) { open hanr ,">", "$logfiles[$fileindex]" or die "could not open logfile .. $!"; while () { if ($_ =~ /$keyword/ ) { print hanw ; #last; --if it will occur only once } } close hanr; $fileindex++; } close hanw; #### if (/$start/i .. /$end/i) # where start and end are your start and end keywords