Perl script to return the number of occurences of multiple lines in afile

S

shane_melville

Hi

I am trying to write a script to return the number of occurences of
certain text/lines/strings in a file. The text is as follows:

Create:subscriber,.........................................................................................................
(blank line)
# sub:success

I have tried the following but it does not work. I am trying to treat
the lines as a string starting with

/Create:subscriber/../sub:success/

but this has not worked. These lines must be together

This is what I have written so far but it does not return the correct
lines

#!/usr/bin/env perl

#use strict;
#use warnings;
sub getlines();
sub getlines1();
my @lines;
my @lines1;
my $lines_ref;
my $lines_ref1;
my $count;
my $count1;
$lines_ref=getlines();
$lines_ref1=getlines1();
@lines=@$lines_ref;
@lines1=@$lines_ref1;
$count=@lines;
$count1=@lines1;
print "The total sub:success count for the file is : $count\n\n
The sub:success strings from the file are as follows: \n\n";
#print join "\n",@lines;

print join "\n",@lines1;
print "The total count for the create subscriber commands is :
$count1\n\n
The create subscriber strings from the file are as follows: \n\n";
#print join "\n",@lines1;

sub getlines()
{
my $file='/data/log/success.log;
open FILE, $file or die "FILE $file NOT FOUND - $!\n";
my @contents=<FILE>;
my @filtered=grep(/sub:success/,@contents);
return \@filtered;

}
sub getlines1()
{
my $file='/data/log/success.log';
my $file='shane.txt';
open FILE, $file or die "FILE $file NOT FOUND - $!\n";
my @contents=<FILE>;
my @filtered1=grep(/Create:subscriber/../sub:success/,@contents);
# my @filtered2=grep(/Create:subscriber/, @contents);
# my @filtered2=grep(/sub:success /,@contents);
return \@filtered1;
}


If anyone can help out drop me a mail

Shane
 
D

davidfilmer

I am trying to write a script to return the number of occurences of
certain text/lines/strings in a file.

perldoc -q count

How can I count the number of occurrences of a substring
within a string?
If anyone can help out drop me a mail

No. That's not how usenet works.
 
M

Michele Dondi

I am trying to write a script to return the number of occurences of
certain text/lines/strings in a file. The text is as follows:

Create:subscriber,.........................................................................................................
(blank line)
# sub:success

I have tried the following but it does not work. I am trying to treat
the lines as a string starting with

/Create:subscriber/../sub:success/

I must say that despite the verbosity of your post I *can't*
understand what you really want. My guess is that that is the number
of lines in blocks beginning with a line which contains
"Create:subscriber" and ending with one which contains "sub:success".
If so, then

perl -lne '$c++ if /Create:subscriber/../sub:success/;
END{print $c}'

should do the trick. Else, you have to explain your *real* goal.


Michele
 
S

shane_melville

I am trying to write a script to return the number of occurences of
certain text/lines/strings in a file. The text is as follows:
Create:subscriber,.........................................................................................................
(blank line)
# sub:success
I have tried the following but it does not work. I am trying to treat
the lines as a string starting with
/Create:subscriber/../sub:success/

I must say that despite the verbosity of your post I *can't*
understand what you really want. My guess is that that is the number
of lines in blocks beginning with a line which contains
"Create:subscriber" and ending with one which contains "sub:success".
If so, then

perl -lne '$c++ if /Create:subscriber/../sub:success/;
END{print $c}'

should do the trick. Else, you have to explain your *real* goal.

Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Hi

THanks very much for the help but I am still having diffficulty
I modified your solution

perl -lne if /CREATE:SUB/../RES=SUCCESS/;
$count++

but it returns all instances of create sub and RES=Success

I need code to just return the count for the number of occurences of
the lines/chunk of text

CREATE:SUB .......
blank line
RES=success
 
S

shane_melville

perldoc -q count

How can I count the number of occurrences of a substring
within a string?


No. That's not how usenet works.

Hi David

I need some sort of code to return the number of occurences of the
text:

Create:sub.....
blank line
res=success

Any ideas?

I have modified the below

perl -lne '$c++ if /Create:subscriber/../sub:success/;
END{print $c}'

to

perl -lne if /Create:subscriber/../sub:success/;
$C++
print Total: "$c";

but it returns all instances of create:sub and all instances of
res:success

I need code to return the count for the occurences in a file of the
following text

Create:sub.....
blank line
res=success
 
N

nolo contendere

Hi David

I need some sort of code to return the number of occurences of the
text:

Create:sub.....
blank line
res=success

Any ideas?

I have modified the below

perl -lne '$c++ if /Create:subscriber/../sub:success/;
  END{print $c}'

to

perl -lne if /Create:subscriber/../sub:success/;
$C++
print Total: "$c";

but it returns all instances of create:sub and all instances of
res:success

I need code to return the count for the occurences in a file of the
following text

Create:sub.....
blank line
res=success

How about this:

#!/usr/bin/perl

use strict; use warnings;
my $c;
my $text = do { local $/; <DATA>; };
while ( $text =~ m/Create\:sub\n\nres=success/g ) {
$c++;
}
print $c, "\n";

__DATA__
Create:sub

res=success
Create:sub

res=success
Create:sub

res=success
Create:sub
not blank!
res=success
blahblah


__OUTPUT__
bash-2.03$ ./regex.pl
3
 
N

nolo contendere

nolo contendere schreef:


Alternative:

  my $text; { local $/; $text = <DATA> }

Is the alternative advantageous in some way, or is it merely a matter
of preference?
 
D

Dr.Ruud

nolo contendere schreef:
Dr.Ruud:

Is the alternative advantageous in some way, or is it merely a matter
of preference?

I assume it uses less memory (no return value to set up), which only
really matters if the file can be big.

Another variant:

my $text;
eval { local $/; $text = <DATA>; 1 } or die;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top