2
Contributors
3
Replies
16
Views
3 Years
Discussion Span
Last Post by 2teez
0

Hi,

How would you write this yourself. i will show one of the ways by which this could be written. How to display the result is left for the OP as an execricse.

use warnings;
use strict;
use Data::Dumper;

my %data;
my $key;

while (<DATA>) {

    if (/User.+?(\S+):/) {
        $key = $1;
    }
    elsif ( /^\s+?floating/ .. /^\s+?"/ ) {
        if (/.+?(Line.+)$/) {
            $data{$key}{counter}++;
            push @{ $data{$key}{line} }, $1;
        }
    }
}

print Dumper \%data;

__DATA__
Users of FEATURE:  (Total of 6 licenses issued;  Total of 6 license in use)

  "FEATURE" v30, vendor: MLM
  floating 

    Line 1 4/17 8:01

  "FEATURE" v30, vendor: MLM
  nodelocked

    Line 2 4/17 8:03
    Line 3 4/17 8:05

  "FEATURE" v30, vendor: MLM
  floating 

    Line 21 4/17 8:20
    Line 21 4/17 8:50
    Line 23 4/17 8:30

Users of FEATURE1:  (Total of 5 licenses issued;  Total of 5 licenses in use)

  "FEATURE1" v31, vendor: MLM
  nodelocked

    Line 12 4/17 8:24   

  "FEATURE1" v30, vendor: MLM
  floating 

    Line 1 4/17 8:45
    Line 2 4/17 8:26

  "FEATURE1" v30, vendor: MLM
  nodelocked

    Line 3 4/17 10:00

  "FEATURE1" v30, vendor: MLM
  floating 

    Line 4 4/17 8:10

Note that I use a filehandler __DATA__. You might have to open your file and read from it instead, using open function in perl and using the while loop like I did.
Use hash data type to get all the values you want and display like you wanted.

Using Data::Dumper in perl to display the following is how your output looks like:

$VAR1 = {
          'FEATURE' => {
                         'counter' => 4,
                         'line' => [
                                     'Line 1 4/17 8:01',
                                     'Line 21 4/17 8:20',
                                     'Line 21 4/17 8:50',
                                     'Line 23 4/17 8:30'
                                   ]
                       },
          'FEATURE1' => {
                          'counter' => 3,
                          'line' => [
                                      'Line 1 4/17 8:45',
                                      'Line 2 4/17 8:26',
                                      'Line 4 4/17 8:10'
                                    ]
                        }
        };

Which is what you wanted.

-1

I forgot to say that "Line" is not a constant word in my file. It is different in different lines

0

I forgot to say that "Line" is not a constant word in my file. It is different in different lines

So how does your dataset looks like then? Moreso, you have to make some effort.

That said you could change line 14 in the code above that is

if (/.+?(Line.+)$/) {

to

if (/.+?(\S.+\d)$/) {

Hope this works.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.