perl - 帮助传递/循环文件中的参数或单独的perl数据结构 [英] perl - help passing/looping in arguments from file or within a separate perl data structure

查看:223
本文介绍了perl - 帮助传递/循环文件中的参数或单独的perl数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个while循环读入一个文件:

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}->{'MyLogdate'} = $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 

此脚本的下一步是:

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

这是我的打印语句:

#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";
}

以下是我添加代码引用{$ server}的输出: p>

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'
                              }

示例输出我正在打印:

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;


推荐答案

你会发现内置变量 $ ARGV 有用:

You will find the builtin variable $ARGV useful:


$ ARGV 包含当从<> / code>。

$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;
        } 
    }
}

这篇关于perl - 帮助传递/循环文件中的参数或单独的perl数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆