Perl 解析 rsync 输出 [英] Perl Parse rsync Output

查看:43
本文介绍了Perl 解析 rsync 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Just a quick Perl question regarding rsync and parsing the stats output.

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

For example, stats such as the below are in a file:

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

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

My weakness begins with regex. I want to extract the information from the line:

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

我想提取所有传递的文件:所以 14 (reg: 3, dir:11).

I want to extract everything passed files: so 14 (reg: 3, dir:11).

我已经对此进行了测试,但它仅适用于数字,我一生都无法弄清楚,我想我需要更多地阅读正则表达式.

I have tested this, but it's for the digits only and I can't for the life of me figure it out, I think I need to read up on regex more.

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

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

This only sets $numfiles as the first digit seen, 14.

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

If anyone can show me how to tackle this, that would be great.

推荐答案

你会从更多地学习正则表达式以及如何使用捕获组中受益,但对于这个特定案例,你可能会想要这样的东西.

You would benefit from studying regular expressions more, and how to use capturing groups, but for this specific case, you'd probably want something like this.

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

这篇关于Perl 解析 rsync 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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