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 8238765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:55:28+00:00 2026-06-07T19:55:28+00:00

I have written a Perl program which will match certain words in a log

  • 0

I have written a Perl program which will match certain words in a log file and store the results in a database. The problem is this program works fine with a small file but doesn’t work with file size ~2GB. Is it size or program need to be changed?

use POSIX qw(strftime);

# load module
use DBI;

open( FILE, "/root/temp.log" ) or die "Unable to open logfile:$!\n";
$count_start   = 0;
$count_interim = 0;
$count_stop    = 0;

while (<FILE>) {
  @test = <FILE>;
  foreach $line (@test) {

    if ( $line =~ m/server start/ ) {

      #print "yes\n";
      $count_start++;
    }
    elsif ( $line =~ m/server interim-update/ ) {
      $count_stop++;
    }
    elsif ( $line =~ m/server stop/ ) {
      $count_interim++;
    }

  }
  print "$count_start\n";
  print "$count_stop\n";
  print "$count_interim\n";
  $now_string = strftime "%b %e %H:%M:%S", localtime;
  print $now_string;

  # connect
  my $dbh = DBI->connect( "DBI:Pg:dbname=postgres;host=localhost",
    "postgres", "postgres", { 'RaiseError' => 1 } );

  # execute INSERT query
  my $rows = $dbh->do(
"insert into radcount (acc,bcc,dcc) Values  ('$count_start','$count_stop','$count_interim')"
  );

  print "$rows row(s) affected\n";

  # clean up
  $dbh->disconnect();

}

close(LOG);
  • 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-07T19:55:29+00:00Added an answer on June 7, 2026 at 7:55 pm

    I have a few comments about your program.

    • Always use strict and use warnings at the start of your program, and declare variables using my at their point of first use

    • Always use lexical filehandles and the three-parameter form of open, and always check the status of an open call

    • You are opening the file using filehandle FILE, but closing LOG

    • Your while statement reads the first line of the file and throws it away

    • @test = <FILE> attempts to read all of the rest of the file into the array. This is what is causing your problem

    • You should connect to the database once and use the same database handle for the rest of the code

    • You should use prepare your statement with placeholders and pass the actual values with execute

    • You are incrementing $count_stop for an interim-update record and $count_interim for a stop record

    • The core module Time::Piece provides a strftime method without the bloat of POSIX

    Here is a modification of your program to show these ideas. I have not set up a log file and database to test it but it looks fine to me and does compile.

    use strict;
    use warnings;
    
    use Time::Piece;
    use DBI;
    
    open my $log, '<', '/root/temp.log' or die "Unable to open log file: $!";
    
    my ($count_start, $count_interim, $count_stop) = (0, 0, 0);
    
    while (<$log>) {
    
      if ( /server start/ ) {
        $count_start++;
      }
      elsif ( /server interim-update/ ) {
        $count_interim++;
      }
      elsif ( /server stop/ ) {
        $count_stop++;
      }
    }
    
    print <<END;
    Start:   $count_start
    Interim: $count_interim
    Stop:    $count_stop
    END
    
    print localtime->strftime("%b %e %H:%M:%S"), "\n";
    
    my $dbh = DBI->connect(
        "DBI:Pg:dbname=postgres;host=localhost", "postgres", "postgres",
        { 'RaiseError' => 1 } );
    
    my $insert = $dbh->prepare('INSERT INTO radcount (acc, bcc, dcc) VALUES (?, ?, ?)');
    my $rows = $insert->execute($count_start, $count_stop, $count_interim);
    
    printf "%d %s affected\n", $rows, $rows == 1 ? 'row' : 'rows';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl program written by someone else. When I run it, it
I have a Perl program that I've written that parses SQL-like statements and creates
I have written a C++ program that reads in a file and outputs the
I have written the following program for automating scp command in perl. #!/usr/bin/expect spawn
The situation I am in is that I have written a perl script which
I have written a XS based Perl module which provides access to functions in
I have written a small script in Perl which I am executing on Windows
I have written a simple module to store and manipulate an ontology which is
I have a perl script written for version 5.6.1 and which has dependencies on
i have written this program to pass a variable in the embedded perl script

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.