CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to search keywords In sequence or according to timing from a text files In Perl ?

 



rocky123
Novice

Feb 22, 2017, 1:41 AM

Post #1 of 4 (1903 views)
How to search keywords In sequence or according to timing from a text files In Perl ? Can't Post

 
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.057570

*******#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 ?


Code
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 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";



Laurent_R
Veteran / Moderator

Feb 22, 2017, 6:18 AM

Post #2 of 4 (1892 views)
Re: [rocky123] How to search keywords In sequence or according to timing from a text files In Perl ? [In reply to] Can't Post

Hi,
The simple answer:

Try to change this line:

Code
	foreach my $kw (keys %$kwhashref)

to this:

Code
	foreach my $kw (sort keys %$kwhashref)


But, less simple answer, given what you do with $kwhashref, I think that you could use an arrayref instead of a hashref. Then probable no sort would be necessary.


BillKSmith
Veteran

Feb 22, 2017, 7:21 AM

Post #3 of 4 (1890 views)
Re: [rocky123] How to search keywords In sequence or according to timing from a text files In Perl ? [In reply to] Can't Post

I disagree with Laurent. I believe that you need the keys sorted in the order of their corresponding values.


Code
    foreach my $kw ( sort { ${$kwhashref}{$a} cmp ${$kwhashref}{$b} } 
keys %$kwhashref )


This sort should work for your sample data but probably not for real data (step-10 would come before step-2). It does demonstrate what you must compare. You may need to write a function to compare them properly. (I cannot do it without knowing more about your data.)
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Feb 22, 2017, 2:49 PM

Post #4 of 4 (1873 views)
Re: [BillKSmith] How to search keywords In sequence or according to timing from a text files In Perl ? [In reply to] Can't Post

You're right, Bill, I was in a rush and looked at the code too quickly and had the impression that it was using only the keys of the hash.


(This post was edited by Laurent_R on Feb 22, 2017, 2:49 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives