filtering a text file
Anonymous Monk
created: 2004-06-14 05:46:28
Dear Monks

I have a text file such as this;
[Microsoft MediaPlayer Settings 1.0]
Script_Path=apps\microsoft\mediaplayersettings\1.0\go\mediaplayersettings10.go
Information=Go Success
Install_Start_Time=05/26/2004 5:55:36 PM
Install_End_Time=05/26/2004 5:55:40 PM
[Microsoft Windows Journal Viewer v1.5]
Script_Path=apps\microsoft\journalviewer\1.5\go\journalviewer.go
UTC_Timestamp=06/03/2003 17:04:10
GoScript_Checksum=66665
Install_Start_Time=05/26/2004 5:58:24 PM
Install_End_Time=05/26/2004 5:58:34 PM
[Company RAC 1.0]
Script_Path=apps\company\remoteaccessclient\1.0\go\uninstallvs.go
UTC_Timestamp=03/03/2003 18:19:05
GoScript_Checksum=175108
Product version=1.0
PM
[Microsoft Resource Kit v1.0]
Script_Path=apps\company\msreskitxp\go\msreskitxp.go
GoScript_Checksum=69503
Product version=1.0
Install_End_Time=05/26/2004 6:00:42 PM
[Microsoft Remote Console Setup v2.02]
Script_Path=apps\company\msreskitxp\go\rcsetup.go
UTC_Timestamp=05/24/2002 01:19:33
GoScript_Checksum=56813
 [Microsoft PowerToys for Windows XP 1.00]
Script_Path=apps\microsoft\powertoysforwindowsxp\1.00\go\powertoysforwindowsxp100.go
I need to capture all the […] followed by the script path. For Example ;
[Microsoft MediaPlayer Settings 1.0]
Script_Path=apps\microsoft\mediaplayersettings\1.0\go\mediaplayersettings10.go

 [Microsoft Resource Kit v1.0]
Script_Path=apps\company\msreskitxp\go\msreskitxp.go

[Microsoft PowerToys for Windows XP 1.00]
Script_Path=apps\microsoft\powertoysforwindowsxp\1.00\go\powertoysforwindowsxp100.go
I wrote this script;
use strict;
use vars qw/@source_go/;
open (LST,"$ARGV[0]/"."c\$/program files/common files/morgan stanley sysadmin/config/go.ini")||die "Cannot find Go.ini file\n";
chomp (my @go = );
for my $data (@go)
{
   my $go_rec;   
   if ($data =~ /^[^a-z]/i)
   {
      #print "\n$data : \n";
      $go_rec->{app} = $data if ($data);
   } 
   if ($data =~ /^Script_path/i)
   {
    #print "$data\n";
    $go_rec->{path} = $data if ($data);
   }
   push (@source_go, $go_rec);     
}
for my $item (@source_go)
{
   next if (! $item->{app});
   print "$item->{app} : ";
   print "$item->{path} \n";
}
However, I do get many empty array elements in my source_go array and I feel there is more effecient way for doing this! I am asking you wise monks if there is a better way to capture the text in between the square bracketts followed by the script path?
Thanks
Re: filtering a text file
created: 2004-06-14 06:26:53
Config::IniFiles does what you want, it seems.

Arjen

Re^2: filtering a text file
created: 2004-06-14 06:35:21
Or, if you don't want to use a module, you could try this solution:
#!/usr/local/bin/perl

use strict;
use warnings;

use Data::Dumper;

chomp(my @data = );
my $in_section = 0;
my @source_go = ();

my $record = {};
foreach my $line (@data) {
    if ($line =~ /^\[([^]]+)\]/ and not $in_section) {
        $record->{app} = $1;
        $in_section = 1;
    }
    elsif ($line =~ /^\[([^]]+)\]/ and $in_section
           and not exists $record->{path})
    {
        # We found a section start, but no Script_Path parameter to go
        # with it.  Warn and continue on the current section.
        warn("No Script_Path for " . $record->{app} . "\n");
        $record->{app} = $1;
        $in_section = 1;
    }
    elsif ($line =~ /^Script_Path=(.+)/) {
        $record->{path} = $1;
        push @source_go, $record;
        $record = {};
        $in_section = 0;
    }
}

print Dumper(\@source_go);
__DATA__
[Microsoft MediaPlayer Settings 1.0]
Script_Path=apps\microsoft\mediaplayersettings\1.0\go\mediaplayersettings10.go
Information=Go Success
Install_Start_Time=05/26/2004 5:55:36 PM
Install_End_Time=05/26/2004 5:55:40 PM
[Microsoft Windows Journal Viewer v1.5]
Script_Path=apps\microsoft\journalviewer\1.5\go\journalviewer.go
UTC_Timestamp=06/03/2003 17:04:10
GoScript_Checksum=66665
Install_Start_Time=05/26/2004 5:58:24 PM
Install_End_Time=05/26/2004 5:58:34 PM
[Company RAC 1.0]
Script_Path=apps\company\remoteaccessclient\1.0\go\uninstallvs.go
UTC_Timestamp=03/03/2003 18:19:05
GoScript_Checksum=175108
Product version=1.0
PM
[Microsoft Resource Kit v1.0]
GoScript_Checksum=69503
Product version=1.0
Install_End_Time=05/26/2004 6:00:42 PM
[Microsoft Remote Console Setup v2.02]
Script_Path=apps\company\msreskitxp\go\rcsetup.go
UTC_Timestamp=05/24/2002 01:19:33
GoScript_Checksum=56813
[Microsoft PowerToys for Windows XP 1.00]
Script_Path=apps\microsoft\powertoysforwindowsxp\1.00\go\powertoysforwindowsxp100.go
It's not extensively tested, but seems to work fine.

Arjen

Re^3: filtering a text file
created: 2004-06-14 17:37:23
THANKS FOR YOUR HELP aragorn.

You tout me something very useful, I will use this code over and over again thought my Perl life, I am sure.

I will post the final code once I finish it tomorrow,...hopefully!

Regards of the highiest order.

perlmonks.org content © perlmonks.org and Anonymous Monk, Aragorn

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03