jcstech has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, I am a newbie to Perl scripting and have a question that I am hoping someone might be able to help answer. I am attempting to create a Perl script that when executed will take the output of the command and arrange it so I can import the data into a monitoring system I have. The command I am needing for the script to open is xgridstatus -h localhost -A and then take the data and format it so I can use the data.

Here is what the output of the above command looks like when ran

{ availableAgentCount = 101; availableProcessorCount = 186; offlineAgentCount = 0; offlineProcessorCount = 0; onlineAgentCount = 101; onlineProcessorCount = 186; totalAgentCount = 101; unavailableAgentCount = 0; unavailableProcessorCount = 0; workingAgentCount = 0; workingAgentPercentage = 0; workingMegaHertz = 0; workingProcessorCount = 0; }

I'm needing to format this data so it looks like this availableAgentCount:101 availableProcessorCount:186 and so on for each line. Any suggestions would be greatly appreciated.

Replies are listed 'Best First'.
Re: Command Parsing
by moritz (Cardinal) on Apr 24, 2012 at 16:54 UTC
Re: Command Parsing
by tobyink (Canon) on Apr 24, 2012 at 18:43 UTC

    Sommint like...?

    local($,,$\) = (":","\n"); foreach (split /\n/, qx{ xgridstatus -h localhost -A }) { if (my @matches = / (\w+) \s* = \s* (\d+) /x) { print @matches; } }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Command Parsing
by 2teez (Vicar) on Apr 24, 2012 at 21:57 UTC

    You can use a 'while' loop to read your output file you have generated, from the CLI, then use a regex to pick out the values you are want like this:

    use warnings; use strict; while (<>) { chomp; if (m/(.+?)\s+?=\s+?(\d+?).?$/) { print $1, " : ", $2, $/; } }

    Or you could decide to pipe in your command in your script, then *work on* each of the line as you want like this: Just run your script i.e >my_script.pl

    #!/usr/bin/perl use warnings; use strict; open my $fh,'-|', q[xgridstatus -h localhost -A] or die "can open this + file: $!"; while (<$fh>) { chomp; if (m/(.+?)\s+?=\s+?(\d+?).?$/) { print $1, " : ", $2, $/; } } close $fh or die "can't close this file:$!";

    Hope it helps

      This is definitely what I am trying to do and the script works great. However would it be possible to only extract the numbers returned and assign each one to a variable so I can format them in the order I need. Example
      print "AGC:$1 APC:$2 OAC:$3" and so on.
      I really do appreciate the help on this, I'm trying to learn perl scripting in my spare time and I'm sure my question shows as a newbie. Thanks

        Why not? To pick out the numbers, you could do this:

        my @arr_data; while (<>) { chomp; if (m/.+?(\d+?).?$/s) { push @arr_data, $1; } } my @assign_var = qw[AGC APC OAC CGA GAC CPA ACP CAO ACO BGH PHY ENG MA +T]; # assumed parameters my %hash_val; @hash_val{@assign_var} = @arr_data; print $_, ":", $hash_val{$_}, $/ foreach sort keys %hash_val;

        Please note that the assumed parameter could also be gotten from within the while loop, it really depend on what you have to work with.
        Hopes this helps

Re: Command Parsing
by aaron_baugher (Curate) on Apr 25, 2012 at 17:46 UTC

    The Fun With Regexes way, though probably not the clearest or most efficient:

    #!/usr/bin/env perl use Modern::Perl; my $text = do { local($/) = undef; <DATA> }; $text =~ s|\s*[{}]\s*||g; $text =~ s|\s*;\s*| |g; $text =~ s|\b(\w)|uc($1)|ge; $text =~ s| = |:|g; $text =~ tr|a-z||d; say $text; __DATA__ { availableAgentCount = 101; availableProcessorCount = 186; offlineAgentCount = 0; offlineProcessorCount = 0; onlineAgentCount = 101; onlineProcessorCount = 186; totalAgentCount = 101; unavailableAgentCount = 0; unavailableProcessorCount = 0; workingAgentCount = 0; workingAgentPercentage = 0; workingMegaHertz = 0; workingProcessorCount = 0; }

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.