Hello,

I have a file as attached. I need to print the lines and count after the pattern "floating" like below. Please help.

FEATURE: 4
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: 3
Line 1 4/17 8:45
Line 2 4/17 8:26
Line 4 4/17 8:10    

Recommended Answers

All 3 Replies

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.