关于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=$1;
}
这只会将$numfiles
设为第一个数字,即14。
如果有人能告诉我如何解决这个问题,那就太棒了。
答案 0 :(得分:2)
您可以从更多地学习正则表达式以及如何使用捕获组中受益,但对于此特定的情况,您可能想要这样的事情。
if ($line =~ /^Number of files:\s+(\d+)\s+\(reg:\s+(\d+),\s+dir:\s+(\d+)\)/) {
$numfiles = $1;
$regfiles = $2;
$dirfiles = $3;
}
答案 1 :(得分:2)
不是仅从一行中提取几个字段,而是将整个输出块解析为有用的数据结构。每个标签都成为一个哈希键,对于前两行的特殊情况,子标签会附加到主标签上以形成唯一键。
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",
}