in reply to Re^2: 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

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
  • Comment on Re^3: Turning regex capture group variables into arrays, then counting the number of objects in the array
  • Download Code

Replies are listed 'Best First'.
Re^4: Turning regex capture group variables into arrays, then counting the number of objects in the array
by Djay (Novice) on Dec 06, 2018 at 08:54 UTC

    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

        I figured that one out after I realised DATA was a handle. My mistake, I'm new to this language :D

        One final thing, as I've tested and tweaked  my %fmt and confirmed I can get this working.

        I have the following outputs

        printf "Statistic.SUCCESSFULL = %d\n",$counts{'Status'}{'0'}; printf "Statistic.PARTIAL = %d\n",$counts{'Status'}{'1'}; printf "Statistic.FAILED = %d\n",$counts{'Status'}{''};

        However I am struggling to find a way to count the failed backups, as failed backups have a status code between 2 and 9999. In Powershell this would be a simple  -gt '1' or to be more specific.

         $PolicyFails = $ColumnName | where {$_.Status -ne $null -and $_.Status -ne "0" -and $_.Status -ne "1"}

        I have tried doing

         printf "Statistic.FAILED = %d\n",$counts{'Status'}{'>1'};

        but this does not work, I assume there is somewhere else I can put the gt or > to tell perl I want to count all numbers in the status column greater than 1?