Gossamer Forum
Home : General : Perl Programming :

Parsing help required

Quote Reply
Parsing help required
Hi

I require a parsing help when parsing and extracting info from a file of following pattern. Please refer to the content in between the '###' boundaries below

####################
Users of spice (total 20 licenses issued; total 0 license in use)
Users of runner (total 10 licenses issued; total 0 license in use)
Users of monitor(total 10 licenses issued; total 2 license in use)
Alex,Mon 20 Jan 2013
Peter,Wed 26 Jan 2013
Users of report (total 15 licenses issued; total 0 license in use)
Users of galaxy(total 10 licenses issued; total 3 license in use)
Martin,Fri 22 Jan 2013
Alex,Mon 21 Jan 2013
Peter,Wed 24 Jan 2013
######################

The file content shows how many licenses for some tools (spice,ruuner,monitor,galaxy) are issued,how many are in use and who all are using if in use.
My task is to extract the info for a given username (say Alex), which all licenses are in use and their availability. If no license is used for that user, then give nothing.

Please help me how can I extract that info for a user only when some licenses are in use.
I have tried to come up with this following code, but need help to go ahead :

Code:
while (<FILE>)
{
if(/^Users of (\w+):/)
{
my $line = $_;
my $licName = $1;
my ($totalIssued,$inUse);
if ($line =~ /total of (\d+) licenses issued/)
{
$totalIssued = $1 ;
print "Total : $totalIssued \n";
}
if ($line =~ /total of (\d+) licenses in use/)
{
$inUse = $1 ;
print "In Use : $inUse \n";
}
}
}
Thanks.
Quote Reply
Re: [xaverian] Parsing help required In reply to
Hi,

It took a little while to get the regex right, but I would use something more like this:

Code:
while ($test =~ /(Users of [a-z0-9 ]+\(total \d+ licenses issued; total \d+ license in use\))(.*?)(?=Users|$)/sg) {
my $line = $1;
my $data = $2;
print qq|FOO: $line with Data: $data\n\n\n\n-------\n\n|;
my ($total,$inuse) = ($line =~ /Users of [a-z0-9 ]+\(total (\d+) licenses issued; total (\d+) license in use/);
print qq|Total licenses: $total, and total in use: $inuse \n |;
}

Should be self explanatory what they do =)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Parsing help required In reply to
Thank you Andy.