rockyurock has asked for the wisdom of the Perl Monks concerning the following question:
My Input file content sample Is :Hello Sample Hello Sample#Step-9:Hello# Bye Sample Bye Sample#Step-3:Bye#
My output generated 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
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 :*******#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
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" +;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to search keywords In sequence or according to timing from a text files In Perl ?
by poj (Abbot) on Feb 22, 2017 at 12:27 UTC | |
by rockyurock (Acolyte) on Feb 22, 2017 at 14:16 UTC | |
by poj (Abbot) on Feb 22, 2017 at 14:27 UTC | |
by rockyurock (Acolyte) on Feb 23, 2017 at 07:15 UTC | |
|
Re: How to search keywords In sequence or according to timing from a text files In Perl ?
by 1nickt (Canon) on Feb 22, 2017 at 11:16 UTC | |
|
Re: How to search keywords In sequence or according to timing from a text files In Perl ?
by BillKSmith (Monsignor) on Feb 22, 2017 at 16:19 UTC |