perl - 帮助从文件或单独的 perl 数据结构中传递/循环参数

发布于 2024-09-13 02:41:35 字数 3674 浏览 0 评论 0原文

我有一个读取单个文件的 while 循环:

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

脚本的执行方式如下,将日志文件作为参数传递:

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

我是否会将这些信息存储在单独的文本文件中以供读取?我会为此使用哈希吗?

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} 的代码时的输出:

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

使用 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;

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 

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2024-09-20 02:41:35

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

$ARGV 包含从 <> 读取时当前文件的名称。

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

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;
        } 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文