Hello Sample Hello Sample#Step-9:Hello# Bye Sample Bye Sample#Step-3:Bye# #### 12-26 13:35:06.057570 236 578 D Hello Sample Hello Sample 12-26 13:35:05.623529 236 578 D Bye Sample Bye Sample #### *******#Step-9:Hello Sample Hello Sample#******* Hello Sample Hello Sample in file main_log, line 1:12-26 13:35:06.057570 *******#Step-3:Bye Sample Bye Sample#******* Bye Sample Bye Sample in file main_log, line 1:12-26 13:35:05.623529 #### printf "\n Starting success message flow\n\n"; # Opening Keyword File here open( my $kw, '<', 'Success_MessageFlow.txt') or die $!; my @keywords = <$kw>; sort @keywords; chomp(@keywords); # remove newlines at the end of keywords # post-processing your keywords file for adding comments my $kwhashref = { map { /^(.*?)(#.*?#)*$/; defined($2) ? ($1 => $2) : ( $1 => undef ) } @keywords }; # get list of files in current directory my @files = ; my $l = 0; # loop over each file to search keywords in foreach my $file (@files) { print "\n Processing with file $file \n"; open(my $fh, '<', $file) or die $!; my @content = <$fh>; sort @content; close($fh); $l++; open my $out_file, '>', "Message_Flow.$l.txt" or die "$!"; foreach my $kw (keys %$kwhashref) { my $search = quotemeta($kw); # otherwise keyword is used as regex, not literally foreach (@content) { # go through every line for this keyword if (/$search/) { printf $out_file "\n*******$kwhashref->{$kw}*******\n"."\n" if defined($kwhashref->{$kw}); printf $out_file '%s in file %s, line %d:%s'.$/, $kw, $file, $l, $_; } } } } printf "Check the output generated In file IMS_Reg_Message_Flow.txt\n";