Reputation: 63
Following up from an earlier question I asked about, I'm trying to read data from two files then parse line by line through one file, looking to match the string contained on each line of the other file. The first is a log file, and the second I will call my key file. I decided to iterate through the key file using a foreach loop then embed inside of it, a while loop that will step through each line of the log. I'm trying to use pattern matching to identify a particular string, but this string is either being surrounded by two plus signs (i.e. +name+) or a plus and an asterisk (i.e. +name*). Unfortunately I'm not having much luck getting the pattern matching to work....I've included the body of my pattern searching code segment below:
foreach $element (@fr_array) {
open (LOGFILE, "<", $logname) or die $!;
while (<LOGFILE>) { #While reading one line at a time from the log....
chomp;
$log_string = <LOGFILE>;
if ($log_string =~ /\+$element\+/) {
$fr_count++;
print "Counter increment since $log_string is the same as $element.";
$log_match++;
}
}
EDIT
Thanks for the feedback, everyone...a couple of the commands I invoked were used to help me trace my way through the program and locate the problem....I took some advice and after a bit more research, am trying to use the grep command instead, but the pattern matching still doesn't seem to be working right. It seems to be resolving as true no matter what I use as the search string. I've been looking over the code all day but I'm not quite sure where to go from here, either. After a failed success with the regular expressions, I decided to go with a different search string to use, this one composed of all literals except for a couple of equals signs. Now, just to clarify, I'm not interested in doing anything with the matches once they're located, I just need to keep track of how many matches there are, hence my use of a counter. Anyway, I'm including an updated code segment of where I am now. I've confirmed that my loops are working correctly, it's just the pattern matching that's the problem. I am getting results now, but the number is not accurate....my test log should only return 1 or 2 but it's spitting out 204 at me.
open (LOGFILE, "<", $logname) or die $!;
while($log_string = <LOGFILE>) {
chomp $log_string;
foreach $element (@fr_array) {
$fr_count++ if (grep {"Action=123 Cls=AM"} $log_string)
Thanks again, all, for the assistance and for the patience. :)
Upvotes: 0
Views: 2873
Reputation: 2543
I'm not 100% sure if I understand the question correctly.
As stated above you can do this very simple with "grep".
What purpose have the 2 counters?
Here's an example with perl.
Inputfile:
123456789abc
234567890abc
345678901abc
bla+bteam+bla
456789012abc
567890123abc
678901234abc
+cteam*
789012345abc
+ateam*
890123456abc
901234567abc
012345678abc
perlcode:
#!/usr/bin/env perl
use strict;
my @tags = ("ateam","bteam");
open LOGFILE, "< inputfile.txt" or dir $!;
my @input = <LOGFILE>;
close LOGFILE;
my ($fr_count, $log_match) = 0;
map {
chomp;
foreach my $tag (@tags) {
/\+$tag[\+\*]/ && do {
print "Counter increment since $_ is the same as $tag.\n";
$fr_count++; $log_match++;
}
}
} @input;
Upvotes: 0
Reputation: 2451
As I pointed out in the comment, you are trying to simulate 'grep -f key_file log_file' command in perl. See if this grep command can help you.
For this perl code you are trying to open the file for each element in @fr_array. It is not needed and makes code inefficient. Moreover you are reading LOGFILE twice in the while loop which needs to be corrected. A example code can be read like below. Note that + or * can be matched by [\+\*] in regex.
open (LOGFILE, "<", $logname) or die $!;
while(my $log_string = <LOGFILE>) {
chomp $log_string;
foreach $element (@fr_array) {
if ($log_string =~ /\+$element[\+\*]/) {
$fr_count++;
print "Counter increment since $log_string is the same as $element.";
$log_match++;
}
}
}
Upvotes: 1