Perl 解析 rsync 输出

Perl Parse rsync Output

只是一个关于 rsync 和解析统计输出的快速 Perl 问题。

例如,如下所示的统计数据在一个文件中:

Number of files: 14 (reg: 3, dir: 11)
Number of created files: 14 (reg: 3, dir: 11)
Number of deleted files: 0
Number of regular files transferred: 3
Total file size: 2,256,078 bytes
Total transferred file size: 2,256,078 bytes
Literal data: 2,256,078 bytes
Matched data: 0 bytes
File list size: 534
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 412
Total bytes received: 2,235,992

sent 412 bytes  received 2,235,992 bytes  894,561.60 bytes/sec
total size is 2,256,078  speedup is 1.01

我的弱点始于正则表达式。我想从行中提取信息:

Number of files: 14 (reg: 3, dir: 11)

我想提取所有通过的文件:所以 14 (reg: 3, dir:11)

我已经对此进行了测试,但它仅适用于数字,我无法在我的生活中弄明白,我想我需要更多地阅读正则表达式。

if($line =~ /Number of files:\s+(\d+)/){
    $numfiles=;
}

这只会将 $numfiles 设置为看到的第一个数字,14。

如果有人能告诉我如何解决这个问题,那就太好了。

更多地研究正则表达式以及如何使用捕获组会对您有所帮助,但对于这种特定情况,您可能需要这样的东西。

if ($line =~ /^Number of files:\s+(\d+)\s+\(reg:\s+(\d+),\s+dir:\s+(\d+)\)/) {
    $numfiles = ;
    $regfiles = ;
    $dirfiles = ;
}

这不是仅仅从一行中提取几个字段,而是将整个输出块解析为有用的数据结构。每个标签都成为一个哈希键,对于前两行的特殊情况,子标签附加到主标签以形成唯一键。

use strict;
use warnings;
use Data::Dump;

my %stats;

while (<DATA>) {
    last unless /\S/;
    chomp;
    my ($lhs, $rhs) = split(/: /, $_, 2);

    if ($rhs =~ /:/) {
        my @parts = split(/\W+/, $rhs);
        $rhs = shift(@parts);

        while (my ($key, $value) = splice(@parts, 0, 2)) {
            $stats{"$lhs - $key"} = $value;
        }
    }

    $stats{$lhs} = $rhs;
}

dd(\%stats);

__DATA__
Number of files: 14 (reg: 3, dir: 11)
Number of created files: 14 (reg: 3, dir: 11)
Number of deleted files: 0
Number of regular files transferred: 3
Total file size: 2,256,078 bytes
Total transferred file size: 2,256,078 bytes
Literal data: 2,256,078 bytes
Matched data: 0 bytes
File list size: 534
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 412
Total bytes received: 2,235,992

sent 412 bytes  received 2,235,992 bytes  894,561.60 bytes/sec
total size is 2,256,078  speedup is 1.01

输出:

{
  "File list generation time"           => "0.001 seconds",
  "File list size"                      => 534,
  "File list transfer time"             => "0.000 seconds",
  "Literal data"                        => "2,256,078 bytes",
  "Matched data"                        => "0 bytes",
  "Number of created files"             => 14,
  "Number of created files - dir"       => 11,
  "Number of created files - reg"       => 3,
  "Number of deleted files"             => 0,
  "Number of files"                     => 14,
  "Number of files - dir"               => 11,
  "Number of files - reg"               => 3,
  "Number of regular files transferred" => 3,
  "Total bytes received"                => "2,235,992",
  "Total bytes sent"                    => 412,
  "Total file size"                     => "2,256,078 bytes",
  "Total transferred file size"         => "2,256,078 bytes",
}