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

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

am 30.12.2007 00:13:54 von 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=;
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=;
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

Re: Perl script to return the number of occurences of multiple lines

am 30.12.2007 00:31:24 von davidfilmer

On Dec 29, 3:13 pm, shane_melvi...@yahoo.com wrote:
> 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.


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

Re: Perl script to return the number of occurences of multiple lines in a file

am 31.12.2007 14:45:32 von Michele Dondi

On Sat, 29 Dec 2007 15:13:54 -0800 (PST), shane_melville@yahoo.com
wrote:

>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^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Perl script to return the number of occurences of multiple lines

am 02.01.2008 11:21:27 von shane_melville

On Dec 31 2007, 1:45 pm, Michele Dondi wrote:
> On Sat, 29 Dec 2007 15:13:54 -0800 (PST), shane_melvi...@yahoo.com
> wrote:
>
> >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^ > .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER > 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

Re: Perl script to return the number of occurences of multiple lines

am 02.01.2008 11:25:07 von shane_melville

On Dec 29 2007, 11:31 pm, davidfil...@gmail.com wrote:
> On Dec 29, 3:13 pm, shane_melvi...@yahoo.com wrote:
>
> > 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.
>
> --
> The best way to get a good answer is to ask a good question.
> David Filmer (http://DavidFilmer.com)

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

Re: Perl script to return the number of occurences of multiple lines

am 02.01.2008 16:59:47 von it_says_BALLS_on_your forehead

On Jan 2, 5:25=A0am, shane_melvi...@yahoo.com wrote:
> On Dec 29 2007, 11:31 pm, davidfil...@gmail.com wrote:
>
>
>
> > On Dec 29, 3:13 pm, shane_melvi...@yahoo.com wrote:
>
> > > I am trying to write a script to return the number of occurences of
> > > certain text/lines/strings in a file.
>
> > perldoc -q count
>
> > =A0 =A0 =A0How can I count the number of occurrences of a substring
> > =A0 =A0 =A0 =A0 =A0 within a string?
>
> > > If anyone can help out drop me a mail
>
> > No. =A0That's not how usenet works.
>
> > --
> > The best way to get a good answer is to ask a good question.
> > David Filmer (http://DavidFilmer.com)
>
> Hi David
>
> I need some sort of code to return the number of occurences of the
> text:
>
> Create:sub.....
> blank line
> res=3Dsuccess
>
> Any ideas?
>
> I have modified the below
>
> perl -lne '$c++ if /Create:subscriber/../sub:success/;
> =A0 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=3Dsuccess

How about this:

#!/usr/bin/perl

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

__DATA__
Create:sub

res=3Dsuccess
Create:sub

res=3Dsuccess
Create:sub

res=3Dsuccess
Create:sub
not blank!
res=3Dsuccess
blahblah


__OUTPUT__
bash-2.03$ ./regex.pl
3

Re: Perl script to return the number of occurences of multiple lines in a file

am 02.01.2008 18:58:50 von rvtol+news

nolo contendere schreef:

> my $text = do { local $/; ; };

Alternative:

my $text; { local $/; $text = }

--
Affijn, Ruud

"Gewoon is een tijger."

Re: Perl script to return the number of occurences of multiple lines

am 02.01.2008 19:18:42 von it_says_BALLS_on_your forehead

On Jan 2, 12:58=A0pm, "Dr.Ruud" wrote:
> nolo contendere schreef:
>
> > my $text =3D do { local $/; ; };
>
> Alternative:
>
> =A0 my $text; { local $/; $text =3D }
>

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

Re: Perl script to return the number of occurences of multiple lines in a file

am 02.01.2008 19:31:13 von rvtol+news

nolo contendere schreef:
> Dr.Ruud:
>> nolo contendere:

>>> my $text = do { local $/; ; };
>>
>> Alternative:
>> my $text; { local $/; $text = }
>
> 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 = ; 1 } or die;

--
Affijn, Ruud

"Gewoon is een tijger."