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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:32:30+00:00 2026-05-16T10:32:30+00:00

I have a perl script that is only keeping the last set of records

  • 0

I have a perl script that is only keeping the last set of records for a named set and I have more than one set of records. So its over writing the data in the hash and just keeping the last set. I need help in printing out all the records.
Thanks!

Here’s a copy of my script:

#!/usr/local/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my ($ServerName)=@ARGV;
my %MyItems;
foreach my $ServerName(@ARGV){
   while (my $line = <>){
     chomp $line;
              if ($line =~ m/.* \w+ \d{2} (\d{2}:\d{2}:\d{2}) \d{4}: ([^:]+):backup:/){
                  my $ServerName = basename $ARGV, '.mydomain.com.backup-software.log'; #$ARGV is reading input from command line
                  my $BckupSet =$2;
                  my $BckupVal=$1;
                  $MyItems{$ServerName}{$BckupSet}->{'1-Server'}    = $ServerName;
                  $MyItems{$ServerName}{$BckupSet}->{'2-BackupSet'} = $BckupSet;
                  $MyItems{$ServerName}{$BckupSet}->{'3-StartTime'} = $BckupVal;

                  if ($line =~ m/(backup-date)[:=](.+)/){
                      my $BckupKey="4-DateStamp";
                      my $BckupVal=$2;
                      $MyItems{$ServerName}{$BckupSet}->{$BckupKey} = $BckupVal;
                  }

                  if ($line =~ m/(backup-time)[:=](.+)/){
                      my $BckupKey="5-Duration";
                      my $BckupVal=$2;
                      $MyItems{$ServerName}{$BckupSet}->{$BckupKey} = $BckupVal;
                  }
                  if ($line =~ m/(backup-size)[:=](.+)/){
                      my $BckupKey="6-Size";
                      my $BckupVal=$2;
                      $MyItems{$ServerName}{$BckupSet}->{$BckupKey} = $BckupVal;
                  }
                  if ($line =~ m/(Backup succeeded)/){
                      my $BckupKey="7-Status";
                      my $BckupVal="Succeeded";
                      $MyItems{$ServerName}{$BckupSet}->{$BckupKey} = $BckupVal;
                  }
                  if ($line =~ m/(ERROR)[:=](.+)/){
                      my $BckupKey="8-Status";
                      my $BckupVal="Unsuccessful";
                      $MyItems{$ServerName}{$BckupSet}->{$BckupKey} = $BckupVal;
                      print "$BckupKey=$BckupVal\n" if debug;
                  }
              }
   } #endwhile
   print Dumper(\%MyItems);
   for my $ServerName(keys%MyItems){
     for my $BckupSet(keys%{$MyItems{$ServerName}}){
       for(sort keys%{$MyItems{$ServerName}{$BckupSet}}){
         #print$_,'=>',$MyItems{$ServerName}{$BckupSet}{$_},';';
         print$_,'=',$MyItems{$ServerName}{$BckupSet}{$_},';';
       }
       print"\n";
     }
   }
} #END foreach

Here’s what it looks like when it dumps:

$VAR1 = {
          'server1.name.colo' => { 
                                        'set1' => {
                                                               '3-StartTime' => '07:08:15',
                                                               '1-Server' => 'server1.name.colo',
                                                               '6-Size' => '72.04 GB',
                                                               '7-Status' => 'Succeeded',
                                                               '4-DateStamp' => '20100820060002',
                                                               '5-Duration' => '01:08:13',
                                                               '2-BackupSet' => 'set1',
                                                               '8-Status' => 'Unsuccessful'
                                                             },
                                        'set2' => {
                                                                '7-Status' => 'Succeeded',
                                                                '6-Size' => '187.24 GB',
                                                                '3-StartTime' => '01:51:25',
                                                                '4-DateStamp' => '20100820000003',
                                                                '1-Server' => 'server1.name.colo',
                                                                '5-Duration' => '01:51:21',
                                                                '2-BackupSet' => 'set2'
                                                              },
                                        'set3' => {
                                                              '3-StartTime' => '23:00:05',
                                                              '4-DateStamp' => '20100814230003',
                                                              '1-Server' => 'server1.name.colo',
                                                              '8-Status' => 'Unsuccessful',
                                                              '2-BackupSet' => 'set3'
                                                            },
                                        'set4' => {
                                                              '7-Status' => 'Succeeded',
                                                              '6-Size' => '427.75 GB',
                                                              '3-StartTime' => '00:43:20',
                                                              '4-DateStamp' => '20100819200004',
                                                              '1-Server' => 'server1.name.colo',
                                                              '5-Duration' => '04:43:14',
                                                              '2-BackupSet' => 'set4'
                                                            },
                                        'set3' => {
                                                              '7-Status' => 'Succeeded',
                                                              '6-Size' => '46.42 GB',
                                                              '3-StartTime' => '04:42:59',
                                                              '4-DateStamp' => '20100820040002',
                                                              '1-Server' => 'server1.name.colo',
                                                              '5-Duration' => '00:42:56',
                                                              '2-BackupSet' => 'set3'
                                                            }
                                      }
        };
  • 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-05-16T10:32:31+00:00Added an answer on May 16, 2026 at 10:32 am

    Based on the debug output, it looks like your problem is here:

    if ($line =~ m/(ERROR)[:=](.+)/){
        my $BckupKey="8-Status";
        my $BckupVal="Unsuccessful";
        $MyItems{$ServerName}{$BckupSet}->{$BckupKey} = $BckupVal;
        print "$BckupKey=$BckupVal\n" if debug;
    }
    

    To save all errors, you’ll need to treat that hash slot as a reference to an array:

    if ($line =~ m/(ERROR)[:=](.+)/){
        my $BckupKey="8-Status";
        my $BckupVal="Unsuccessful";
        push @{ $MyItems{$ServerName}{$BckupSet}{$BckupKey} } => $BckupVal;
        print "$BckupKey=$BckupVal\n" if debug;
    }
    

    In your dump, the 8-Status values will resemble

    '8-Status' => [ 'Unsuccessful', 'Other error', 'Et cetera' ],

    If you want to loop over them later, you’d do something like

    foreach my $err (@{ $MyItems{$ServerName}{$BckupSet}{$BckupKey} }) {
        print "got $err\n";
    }
    

    To get only the first, you’d write

    print $MyItems{$ServerName}{$BckupSet}{$BckupKey}[0], "\n";
    

    Another issue is

    foreach my $ServerName(@ARGV){
       while (my $line = <>){
    

    Realize that while (<>) { ... } implicitly loops over all files named in @ARGV, so nesting it inside a loop over @ARGV doesn’t quite make sense. If your command line is of the form

    $ readlogs server1 server2 server3 log1 log2

    then you’d want to first remove from @ARGV the servers using shift. Distinguishing arguments the user intends as server hostnames could be tricky. One convention is using -- to signal the end of option processing, so you might

    my @servers;
    while (@ARGV) {
      my $server = shift;
      last if $server eq "--"
      push @servers => $server;
    }
    
    die "Usage: $0 server .. -- log ..\n" unless @ARGV;
    
    while (<>) {
      # ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script that launches 2 threads,one for each processor. I need
I've got a Perl script that as one of its final steps creates a
I am trying to insure that only one instance of a perl script can
I have a Perl script that does various installation steps to set up a
I have a Perl script that takes user input and creates another script that
I have a Perl script that uses WWW::Mechanize to read from a file and
I have a Perl script that takes text values from a MySQL table and
I have a Perl script that requires a couple of plugins, for istance nmap.
I have a Perl script that will execute three applications. All of it have
I have a perl script that's reading an INI file like this: [placeholder_title] Hostname

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.