开发者

perl - help passing/looping in arguments from file or within a separate perl data structure

I have a while loop that reads in a single file:

my %MyItems = ();
while (my $line = <>)
{
    chomp $line;

    if ($line =~ m/(.* $mon $day) \d{2}:\d{2}:\d{2} $year: ([^:]+):backup:/)   
    {      
        my $BckupDate="$1 $year";               
        my $BckupSet =$2;               

        $MyItems{$BckupSet}->{'MyLogd开发者_如何学Pythonate'} = $BckupDate; 
        $MyItems{$BckupSet}->{'MyDataset'} = $BckupSet; 

        if ($line =~ m/(ERROR|backup-size|backup-time|backup-status)[:=](.+)/) 
        {
            my $BckupKey=$1;   
            my $BckupVal=$2; 
            $MyItems{$BckupSet}->{$BckupKey} = $BckupVal;
        } 
    }
}

The script is executed like so, passing in the log file as an argument:

./hashtst3.pl /var/log/server1.log 

My next progression of this script is to:

1-read more than 1 log file 
2-store the hostname of where the logfile is from
3-eventually print this data out with the other hashes  

Would I store this information in a separate text file to read in? Would I use a hash for this?

server1 /var/log/server1.log
server2 /var/log/server2.log
server3 /var/log/server3.log
...

here are my print statements:

#Final print format
print "### Verify Final Output\n";
for my $PrintItems (sort keys %MyItems)
{
    my %PrintVal = %{$MyItems{$PrintItems}};
    for my $PrintKey (sort keys %PrintVal)
    {
        printf "%s=>%s;\n", $PrintKey, $PrintVal{$PrintKey};
    }
    print "\n";
}

Here is my output as of adding code referencing {$server}:

### Verify Final Output
abc2.cfl.mil.mad=>HASH(0x9da338c);
abc3.mil.mad=>HASH(0x9e20708);
abc4.mad_lvm=>HASH(0x9e20660);
abc1.mil.mad=>HASH(0x9e20774);

backup-size=>187.24 GB;
backup-status=>Backup succeeded;
backup-time=>01:54:27;

backup-size=>46.07 GB;
backup-status=>Backup succeeded;
backup-time=>00:41:06;

backup-size=>422.99 GB;
backup-status=>Backup succeeded;
backup-time=>04:48:50;

Adding current output per help, using dumper:

$VAR1 = 'server1';
$VAR2 = {
          'abc1.mil.mad' => {    'ERROR' => ' Error mesg here',
                                 'MyLogdate' => 'Fri Aug 06 2010',
                                 'MyDataset' => 'abc1.mil.mad'
                               },
          'abc2.cfl.mil.mad' => {
                                  'backup-size' => '187.24 GB',
                                  'MyLogdate' => 'Fri Aug 06 2010',
                                  'backup-status' => 'Backup succeeded',
                                  'backup-time' => '01:54:27',
                                  'MyDataset' => 'abc2.cfl.mil.mad'
                                },
          'abc3.mil.mad' => {
                                'backup-size' => '46.07 GB',
                                'MyLogdate' => 'Fri Aug 06 2010',
                                'backup-status' => 'Backup succeeded',
                                'backup-time' => '00:41:06',
                                'MyDataset' => 'abc3.mil.mad'
                              },
          'abc4.mad_lvm' => { 
                                'backup-size' => '422.99 GB',
                                'MyLogdate' => 'Fri Aug 06 2010',
                                'backup-status' => 'Backup succeeded',
                                'backup-time' => '04:48:50',
                                'MyDataset' => 'abc4.mad_lvm'
                              }

Sample output I am looking to print:

MyHost=>Server1;MyDataset=>abc2.cfl.mil.mad;MyLogdate=>Fri Aug 06 2010;backup-size=>187.24 GB;backup-status=>Backup succeeded;backup-time=>01:54:27;


You will find the builtin variable $ARGV useful:

$ARGV contains the name of the current file when reading from <>.

use strict; use warnings;

use File::Basename;
my %MyItems;

while (my $line = <>) {
    chomp $line;

    if ($line =~ m/(.* $mon $day) \d{2}:\d{2}:\d{2} $year: ([^:]+):backup:/)
    {
        my $server = basename $ARGV, '.log';
        my $BckupDate ="$1 $year";               
        my $BckupSet = $2;               

        $MyItems{$server}{$BckupSet}{MyLogdate} = $BckupDate; 
        $MyItems{$server}{$BckupSet}{MyDataset} = $BckupSet; 

        if ($line =~ m/(ERROR|backup-size|backup-time|backup-status)[:=](.+)/) 
        {
            my $BckupKey=$1;   
            my $BckupVal=$2;
            # *** Don't forget *** # 
            $MyItems{$server}{$BckupSet}{$BckupKey} = $BckupVal;
        } 
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜