【问题标题】:Perl extracting data from a file based on conditionPerl根据条件从文件中提取数据
【发布时间】:2013-07-10 00:27:08
【问题描述】:

我有一个如下所示的日志文件:

 874899 root@commands to execute some files
    Exit Status : 0
    Exit time   : Sun May  5 18:19:39 2013
 874923 root@commands to execute some files
    Exit Status : 2
    Exit time   : Sun May  5 18:19:42 2013

我有一个脚本,它查看一个模式并返回该匹配模式的下一行。脚本如下:

 open(FH,'log.txt');
 while ($line = <FH>) {
     if ($line =~ /Exit Status/) {
         print "$line";
         print scalar <FH>;
     }
}

我需要您输入关于我应该如何执行此操作的信息,以便它匹配 Exit status(在本例中为 2)并保存 874923 行以及命令(在本例中)和 Exit Time作为两个独立的变量。

请纠正我,因为我是 perl 的新手。

【问题讨论】:

  • Exit Time 作为两个独立的变量是什么意思?您希望它的文本在一个变量中,而日期/时间在另一个变量中?
  • @JasonGray 感谢您的快速回复。在这种情况下,退出时间只是一个变量。但是根据日志文件是变量
  • 从您的问题中不清楚您的日志文件包含什么。我认为# Consists of three lines output 不在文件中,但{...} 在那里?是否有三行数据包括Exit StatusExit times,还是每条记录总共有五行?你不能只发布一些实时数据吗?理解起来会简单得多。
  • 将标量变量放在引号内几乎是不对的。 print $line 是正确的。
  • @Borodin 谢谢您的回复...我已经编辑了我的上述问题..如果退出状态条件等于 2..I,我需要 2 个变量可以保存上述行 874923 和退出状态希望这会有所帮助..谢谢

标签: perl conditional-statements extraction file-processing


【解决方案1】:

你的代码可能是这样的:

 use Data::Dumper;
 open(FH,'inlog.txt');

 my @stat;

 my ($exitstatus, $exitstatusval, $exittime, $exittimeval, $exitcommands);
 while ($line = <FH>) {
        if ($line =~ m/\d+\s+.*@.*/) {
            $exitcommands = $line;
        }
        if ($line =~ /Exit Status/) {
            ($exitstatus, $exitstatusval) = split(':',$line);
            next;
        }
        if ($line =~ /Exit time/ and $exitstatusval == 2) {
            ($exittime, $exittimeval) = split(': ',$line);
             push (@stat, {
                commands => $exitcommands,
                time => $exittimeval
                });
        }
}

print(Dumper(\@stat));

输出: 因此,这将为退出状态为 2 的条目打印“arrayref of hashrefs”

  $VAR1 = [
          {
            'time' => 'Sun May  5 18:19:42 2013 ',
            'commands' => '874923 root@commands to execute some files '
          },
          {
            'time' => 'Sun May  4 18:19:42 2013',
            'commands' => '874613 root@commands to execute some files '
          }
        ];

【讨论】:

  • 感谢您的回复...您的脚本仅针对有限数量的条目区分退出状态 0、1 和 2。如果有一个包含大量此类条目的文件,我该怎么办只需要退出状态的信息:2。谢谢。
  • 感谢您的回复。但是您的代码没有针对整个文件进行迭代..我只得到一个退出状态的值:2,虽然它附加了许多变量..你能帮我知道我在哪里想错了吗?谢谢。
  • @deep 已编辑,请立即重新检查
  • 像冠军一样工作..谢谢@Drt。此外,需要进行哪些更改才能使其与哈希一起使用。由于非唯一键值对是否存在差异?
  • @deep 使用哈希,您可以将单个键作为 2 并且它将指向哈希引用的数组引用。我直接使用了hashref的arrayref。您可以将 @stat \@stat 的引用作为键 2 的值,这样它也可以在这种情况下工作。
【解决方案2】:

这就是我的做法......

use Data::Dumper;

open(FH,'<','log.txt');
my $current_log;
my @logs;
while (my $line = <FH>) {
  if($line =~ /^\s*(\d+)\sroot\@(.*)/) {
    if($current_log) {
      push @logs,$current_log;
    }
    $current_log = {};
    $current_log->{pid} = $1;
    $current_log->{command} = $2;
  }
  if ($line =~ /Exit Status\s*:\s*(\d+)/) {
    $current_log->{exit_status} = $1;
  }
  if($line =~ /Exit time\s*:\s*(.+)$/) {
    $current_log->{exit_time} = $1;
  }
}
if($current_log) {
  push @logs,$current_log;
}

print Dumper \@logs;

应该打印出以下内容:

$VAR1 = [
      {
        'exit_time' => 'Sun May  5 18:19:39 2013',
        'pid' => '874899',
        'exit_status' => '0',
        'command' => 'commands to execute some files'
      },
      {
        'exit_time' => 'Sun May  5 18:19:42 2013',
        'pid' => '874923',
        'exit_status' => '2',
        'command' => 'commands to execute some files'
      }
    ];

【讨论】:

  • @Gordolia 谢谢你的时间和回复:-)
【解决方案3】:

在hash的帮助下,我用的是这个:

use Data::Dumper;
open(FH,'inlog.txt');

my %stat;

my ($exitstatus, $exitstatusval, $exittime, $exittimeval, $exitcommands);
while ($line = <FH>) {
    if ($line =~ m/^(\d+)\s+.*@(.*)/) {
        $exitcommands = $2;
        $qbsid= $1;
    }
    if ($line =~ /Exit Status/) {
        ($exitstatus, $exitstatusval) = split(':',$line);
        next;
    }
    if ($line =~ /Exit time/ and $exitstatusval == 2) {
        ($exittime, $exittimeval) = split(': ',$line);
         $stat{$qbsid} = {
            commands => $exitcommands,
            time     => $exittimeval
            };
    }

}

  print(Dumper(\%stat));

【讨论】:

    猜你喜欢
    • 2020-03-02
    • 2018-09-25
    • 2022-01-25
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多