Search code examples
regexperlstatisticsrsync

Perl Parse rsync Output


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)

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

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

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


Solution

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