In my below program , I want to search all the keywords provided In text file named - Success_MessageFlow.txt from a list of file named with *main_log and I am getting the output In each different file named Message_Flow.$l.txt according to the no of files present In a directory My keyword file sample contains :
Hello Sample Hello Sample#Step-9:Hello# Bye Sample Bye Sample#Step-3:Bye#
My Input file content sample Is :
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
My output generated Is :
*******#Step-9:Hello Sample Hello Sample#******* Hello Sample Hello Sample in file main_log, line 1:12-26 13:35:06.0575 +70 *******#Step-3:Bye Sample Bye Sample#******* Bye Sample Bye Sample in file main_log, line 1:12-26 13:35:05.623529
Issue : Though both Step-9 and Step-3 are searched correctly but I want searching to be In order of timing or Step-3 should be printed In my output file before Step-9 as step-3 keyword occurrence was before (12:35:05) than the Step-9 (12:35:06) Can anyone please help to suggest any modification in my program that can help ? My Program :
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 = <main_log*>; 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 re +gex, 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" +;

In reply to How to search keywords In sequence or according to timing from a text files In Perl ? by rockyurock

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.