Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:38:24+00:00 2026-06-10T05:38:24+00:00

Following up from an earlier question I asked about, I’m trying to read data

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:38:25+00:00Added an answer on June 10, 2026 at 5:38 am

    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++;
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from my earlier question about creating Address Books (many thanks Peter!), I
Following on from an earlier question I'm trying to get multiple (widgets or in
Earlier today I asked a question about configuring log4net from code and got an
Following up from an earlier question on extracting the n'th regex match , I
Following on from my earlier question , I am still having issues with loading
In an earlier question, I asked about typecasting pointers, but was directed to the
Following on from my earlier question, I have decided to have a go at
(Split from an earlier question). Does anyone know what's going on here? The following
Following up from the question I made earlier, I made the query below but
I asked an earlier question about absolute and relative div positioning , but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.