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

Discussion in 'Perl Misc' started by shane_melville@yahoo.com, Dec 29, 2007.

  1. Guest

    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
     
    , Dec 29, 2007
    #1
    1. Advertising

  2. Guest

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

    On Dec 29, 3:13 pm, 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)
     
    , Dec 29, 2007
    #2
    1. Advertising

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

    On Sat, 29 Dec 2007 15:13:54 -0800 (PST),
    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^<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,
     
    Michele Dondi, Dec 31, 2007
    #3
  4. Guest

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

    On Dec 31 2007, 1:45 pm, Michele Dondi <> wrote:
    > On Sat, 29 Dec 2007 15:13:54 -0800 (PST),
    > 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^<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
     
    , Jan 2, 2008
    #4
  5. Guest

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

    On Dec 29 2007, 11:31 pm, wrote:
    > On Dec 29, 3:13 pm, 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
     
    , Jan 2, 2008
    #5
  6. Re: Perl script to return the number of occurences of multiple linesin a file

    On Jan 2, 5:25 am, wrote:
    > On Dec 29 2007, 11:31 pm, wrote:
    >
    >
    >
    > > On Dec 29, 3:13 pm, 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


    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
     
    nolo contendere, Jan 2, 2008
    #6
  7. Dr.Ruud Guest

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

    nolo contendere schreef:

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


    Alternative:

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

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jan 2, 2008
    #7
  8. Re: Perl script to return the number of occurences of multiple linesin a file

    On Jan 2, 12:58 pm, "Dr.Ruud" <> wrote:
    > nolo contendere schreef:
    >
    > > my $text = do { local $/; <DATA>; };

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


    Is the alternative advantageous in some way, or is it merely a matter
    of preference?
     
    nolo contendere, Jan 2, 2008
    #8
  9. Dr.Ruud Guest

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

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


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

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

    >
    > 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;

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jan 2, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chuck Amadi
    Replies:
    0
    Views:
    524
    Chuck Amadi
    Jun 22, 2004
  2. Larry
    Replies:
    5
    Views:
    786
    Bernard
    Mar 13, 2008
  3. Seid Mohammed
    Replies:
    0
    Views:
    282
    Seid Mohammed
    Nov 21, 2008
  4. Phil Tomson
    Replies:
    1
    Views:
    107
    Phil Tomson
    Dec 31, 2007
  5. Markus Fischer
    Replies:
    1
    Views:
    373
    Markus Fischer
    May 31, 2011
Loading...

Share This Page