in reply to Re: Regex to extract number from text file
in thread Regex to extract number from text file

It appears you are trying to parse the output of the FlexLM 'lmstat' command. I've tackled that problem before.

A few details worth noting that I can think of off hand:

  • The 's' after the word licenses should be marked optional with a ? in the regexp.
  • Licenses can be "Reserved" and "Queued" as well, if my memory served, those are the strings one sees.
  • The lmstat output itself can vary in format on a per-feature basis. The lmstat tool is merely passing on data from the "vendor daemons" and the format varies quite a bit.

    The solution I found first way back in 1998-1999 that worked the best was not to solve the problem in a single Regexp, but rather to use multiple Regexp looking for pieces of the puzzle.

    To be more specific:

  • Parse the license/feature name out of the string, and strip out the part that follows with the 'usage' data into a new string.
  • Parse the 'usage' string and look for the pieces you need. Handle the variations if they occur in your data. Code example:

    #!perl # sample string. The code below could be looped as well. # i.e. within while($string = <LMSTAT>){ } my $string = 'Users of ClearQuest: (Total of 18 licenses issued; Total + of 12 licenses in use)'; if($string =~ m/Users? of (\w+):\s+(\(.+\))/){ # must test for successful pattern match # or value may mistakenly be set to previous successful match! my $feature =$1; my $usage = $2; my $num_used=0; my $num_total=0; my $num_queued=0; if($usage =~ m/(\d+)\slicenses? (in use|Used)/ # handles variation +s in Used Licenses ){ $num_used=$1; } if($usage =~ m/(\d+)\slicenses? (issued|available)/ # handles vari +ations in Total Licenses ){ $num_total=$1; } if($usage =~ m/(\d+)\slicenses? (queued)/ # handles Queued license +s ){ $num_queued=$1; } # more IF statements to test for queued, reserved, etc. print "lmstat output: $string\n"; print "feature name: $feature\n"; print "usage string: $usage\n\n"; print "Total $feature Licenses: $num_total\n"; print "Total $feature Used Licenses: $num_used\n"; print "Total $feature Queued Licenses: $num_queued\n"; my $num_free=$num_total-$num_used-$num_queued; print "Approximate Free $feature Licenses: $num_free\n"; } else { # not a "Usage" line. Perhaps parse out # the USERNAMES of each used license here... }

    This should directly solve your problem, and prepare you for any hangups that may await you. Like Perl, with FlexLM there is 'more than one way to do it' so keep this in mind.

    If you need further help, you can message me privately. I've used Perl to populate a data warehouse with these sort of data. Getting control over license usage is a great way to reduce software costs.

    spectre#9 -- "Strictly speaking, there are no enlightened people, there is only enlightened activity." -- Shunryu Suzuki
  • Replies are listed 'Best First'.
    Re^3: Regex to extract number from text file
    by premal (Acolyte) on Feb 19, 2009 at 05:52 UTC
      Hi Thanks a lot for your help. I will try your solution and you have pertactly understood my problem.