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

Dear Perl experts,

I have a data file which contains media per volume pool as shown below "########" line. If a name eg "CatalogBackup Pool" has 9 fields output, then it means this pool has some medias in it. I need to find the count of such lists.

The output need to be something like:

CatalogBackup pool 8 DataStore pool 0 MonthEndPool pool 0 NetBackup pool 0 None pool 0 ScratchPool pool 3

Is there any way in perl to do this?

###########################################

Here is the source data. :

###################################################################### +############################## # cat REPORT1 CatalogBackup pool 000572 HCART TLD 0 111 - - - AVAILA +BLE 002315 HCART TLD 0 355 - - - AVAILA +BLE 002316 HCART TLD 0 311 - - - AVAILA +BLE 002383 HCART TLD 0 560 - - - AVAILA +BLE 002386 HCART TLD 0 440 - - - AVAILA +BLE 002387 HCART TLD 0 537 - - - AVAILA +BLE 002390 HCART TLD 0 595 - - - AVAILA +BLE 002392 HCART TLD 0 680 - - - AVAILA +BLE DataStore pool MonthEndPool pool NetBackup pool None pool ScratchPool pool 003125 HCART TLD 0 490 - - - AVAILA +BLE 003126 HCART TLD 0 488 - - - AVAILA +BLE 003150 HCART TLD 0 480 - - - AVAILA +BLE ###################################################################### +##############################

Replies are listed 'Best First'.
Re: How to generate a report based on keyword?
by SuicideJunkie (Vicar) on May 17, 2010 at 19:06 UTC

    There are lots of ways to do anything in any language. Of course there is a way to do things in perl.

    Perhaps you need help with the basic logic flow? Break it up into simple steps:

    1. Step through the file one line at a time and apply regexes
    2. If you see a category change, remember that.
    3. If you see a resource is available, increment the tally for the current category. (Use a Hash, since categories are arbitrary strings, rather than sequential numbers)
    4. When done, print the totals.

Re: How to generate a report based on keyword?
by NetWallah (Canon) on May 17, 2010 at 19:46 UTC
    Here you go - (windows solution - change to single quote for linux):
    perl -ne "$h{$c=$1}=0 if m/^([\w\s]+pool)$/;m/^\d{6}\s+\w{4}/ and $h{$ +c}++;END{print qq|$_\t$h{$_}\n| for sort keys %h}" FILENAME

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

      @NetWallah: Your solution worked perfectly fine and the way I wanted. Thanks a lot! Much appreciated.

      #!/bin/ksh perl -ne '$h{$c=$1}=0 if m/^([\w\s]+pool)$/;m/^\d{6}\s+\w{4}/ and $h{$ +c}++;END{print qq|$_\t$h{$_}\n| for sort keys %h}' REPORT1