in reply to Re: Turning regex capture group variables into arrays, then counting the number of objects in the array
in thread Turning regex capture group variables into arrays, then counting the number of objects in the array

Its funny, this is actually very close to the first portion of my Powershell script. Does this allow me to call and count the matches in an individual capture group? To do this in Powershell (sorry to bring this here but I find it easier to explain code in code form)
$output = ./bpdbjobs $Results = @() $ColumnName = @() foreach ($match in $OUTPUT) { $matches = $null $match -match "(?<jobID>\d+)?\s+(?<Type>(\b[^\d\W]+\b)|(\b[^\d\W]+ +\b\s+\b[^\d\W]+\b))?\s+(?<State>(Done)|(Active)|(\w+\w+`-\w`-+))?\s+( +?<Status>\d+)?\s+(?<Policy>(\w+)|(\w+`_\w+)|(\w+`_\w+`_\w+))?\s+(?<Sc +hedule>(\b[^\d\W]+\b\-\b[^\d\W]+\b)|(\-)|(\b[^\d\W]+\b))?\s+(?<Client +>(\w+\.\w+\.\w+)|(\w+))?\s+(?<Dest_Media_Svr>(\w+\.\w+\.\w+)|(\w+))?\ +s+(?<Active_PID>\d+)?\s+(?<FATPipe>\b[^\d\W]+\b)?" $Results+=$matches } foreach ($result in $results) { $Object = New-Object psobject -Property @{ JobID = $Result.jobID Type = $Result.Type State = $Result.State Status = $Result.Status Policy = $Result.Policy Schedule = $Result.Schedule Client = $Result.Client Dest_media_svr = $Result.dest_media_svr Active_PID = $Result.Active_PID FATPipe = $Result.FATPipe } $ColumnName += $Object }
Powershell already understands that $Result.jobID is referring to the jobID capture group. All this does is put the capture group and results of said capture into an object which I can put into a variable and use any time using the below code as an example
$Successful = ($ColumnName | where {$_.Status -eq "0"}).count
This creates a variable which is formed out of the previous codes variable, it then pulls out only the matches in the Status column ($_.Status) and counts the ones that match the value 0.
  • Comment on Re^2: Turning regex capture group variables into arrays, then counting the number of objects in the array
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: Turning regex capture group variables into arrays, then counting the number of objects in the array
by poj (Abbot) on Dec 04, 2018 at 15:27 UTC

    If you have fixed format records, then perhaps using unpack is a simpler option. For example

    #!/usr/bin/perl use strict; use Data::Dumper ; my $fmt = 'A13 A8 A8 A10 A6 A20 A6 A12 A16 A5'; my %counts = (); my @col = ('JobID','Col2','Type','State', 'Status','Policy','Schedule','Client', 'Dest Media Svr','Active PID'); # 10 cols while (<DATA>){ next unless /\S/; # skip blank lines next if /^\s+JobID/; # skip header chomp; my @f = unpack $fmt,$_; s/^\s+|\s+$//g for @f; # trim spaces # count each column for my $n (0..$#col){ ++$counts{$col[$n]}{$f[$n]}; } print join "\|",@f,"\n"; # check } print Dumper \%counts; printf "Succesfull = %d\n",$counts{'Status'}{'0'}; __DATA__ JobID Type State Status Policy Schedule + Client Dest Media Svr Active PID 41735 Backup Done 0 Policy_name_here daily + hostname001 MediaSvr1 8100 41734 Backup Done 0 Policy_name_here daily + hostname002 MediaSvr1 7803 41733 Backup Done 0 Policy_name_here daily + hostname004 MediaSvr1 7785 41732 Backup Done 0 Policy_name_here daily + hostname005 MediaSvr1 27697 41731 Backup Done 0 Folicy_name_here daily + hostname006 MediaSvr1 27523 41730 Backup Done 0 Policy_name_here daily + hostname007 MediaSvr1 27834 41729 Backup Done 0 Policy_name_here - + hostname008 MediaSvr1 27681 41728 Backup Done 0 Policy_name_here - + hostname009 MediaSvr1 27496 41727 Catalog Backup Done 0 catalog full + hostname010 MediaSvr1 27347 41712 Catalog Backup Done 0 catalog - + hostname004 30564
    poj

      I've only just had chance to take a look at this, and this is almost certainly what I've been looking for - the output is perfect, IF I can pass the command output through to _DATA_

      The commands output is ALWAYS the same formatting, different data so in theory this should work? If so, can you explain a way for me to pass the commands output over to _DATA_ for unpack?

        I used the filehandle DATA as a convenient way to include some test lines in a Simple, Self Contained Example. You can either do as markong suggested and populate an array with your output

        my @output = `bpdbjobs`; #while (<DATA>){ for (@output) { # process lines }

        or if you have the output in a text file, open a filehandle to that

        my $filename = 'output.txt'; open my $fh,'<',$filename or die "Could not open $filename : $!"; while (<$fh>){ # process lines }

        or you may be able to pipe the output directly from your program. see Calling External Commands More Safely

        open my $fh, '-|', 'bpdbjobs' or die $!; while (<$fh>){ # process lines } close $fh or die $! ? $! : $?;
        Updated ; last example corrected backticks to single quote
        poj
Re^3: Turning regex capture group variables into arrays, then counting the number of objects in the array
by markong (Pilgrim) on Dec 04, 2018 at 15:56 UTC

    I have zero PowerShell knowledge, but from what you describe you probably want to use the named capture groups feature (?<NAME>...) (the original regex you posted didn't contain any...).
    This is still a capture group just like a regular parenthesized grouping, but its name is NAME.

    I'd recommend you to look at some examples and while you're at it, you should also read part 1 of that wonderful tutorial to understand how to access the various bits of information from a successful match!

    It's all there!