Multiple Line Pattern Match problem

Discussion in 'Perl Misc' started by samuel, May 31, 2007.

  1. samuel

    samuel Guest

    Hi All,

    Now I need to analyze a file which is composed of several blocks ,
    which is defined as below :


    Start
    <content>
    <content>
    .......
    <content>
    End


    And I need to capture all the blocks which contains several
    specific keywords "dma" in the content part.

    The perl script used as below :

    undef $/; # each read is whole file
    while (<>) {
    # $mycmd ='^START(.*?)dma(.*?)^End/sgm){
    print $&;
    }
    }


    However, for below blocks :
    Start
    cpu
    End
    Start
    dma
    End

    The RegExp of perl will take these two blocks as one match and print
    out. This is not what I want.

    I just need the second block print out and don't the first one not.

    Does anyone know how to handle this in perl script ?

    Many thanks in advance .

    Samuel
     
    samuel, May 31, 2007
    #1
    1. Advertising

  2. samuel

    samuel Guest

    On May 31, 5:40 pm, Aukjan van Belkum <>
    wrote:
    > samuel wrote:
    > > Hi All,

    >
    > > Now I need to analyze a file which is composed of several blocks ,
    > > which is defined as below :

    >
    > > Start
    > > <content>
    > > <content>
    > > ......
    > > <content>
    > > End

    >
    > This can be matched as follows
    >
    > # open file, don't read as a whole
    >
    > while (<IN>){
    >
    > ## Read all blocks starting with Start and end at first End
    > if ( /^Start/ .. /^End/ ){
    > print if /dma/;
    > }
    >
    > }
    >
    > Check for more info 'man perlop' => look for the range operator.
    >
    > Aukjan.


    Thanks Aukjan,

    This did work and can print out all the lines where there are keyword
    dma in.

    But How to do if the block is like below :

    Start
    <content_without_keyword_dma>
    ......
    <content_with_keyword_dma>
    ......
    <content_without_keyword_dma>
    End

    And I need to print out both all the contents (both lines w and w/o
    keyword dma) for the block where there are <content_with_keyword_dma>
    there ?

    Samuel
     
    samuel, May 31, 2007
    #2
    1. Advertising

  3. samuel

    Brad Baxter Guest

    On May 31, 9:25 am, samuel <> wrote:
    > But How to do if the block is like below :
    >
    > Start
    > <content_without_keyword_dma>
    > ......
    > <content_with_keyword_dma>
    > ......
    > <content_without_keyword_dma>
    > End
    >
    > And I need to print out both all the contents (both lines w and w/o
    > keyword dma) for the block where there are <content_with_keyword_dma>
    > there ?


    my $x = do { local $/; <DATA> };
    while ( $x =~ /^Start(.*?)^End/smg ) {
    defined and /dma/ and print for my $y = $1;
    }
    __DATA__
    Start
    cpu
    End
    Start
    dma
    End
    Start
    <content_without_keyword_dma>
    .......
    <content_with_keyword_dma>
    .......
    <content_without_keyword_dma>
    End

    --
    Brad
     
    Brad Baxter, May 31, 2007
    #3
  4. On May 31, 7:26 am, Aukjan van Belkum <>
    wrote:
    > samuel wrote:
    > > On May 31, 5:40 pm, Aukjan van Belkum <>
    > > wrote:
    > >> samuel wrote:
    > >>> Hi All,
    > >>> Now I need to analyze a file which is composed of several blocks ,
    > >>> which is defined as below :
    > >>> Start
    > >>> <content>
    > >>> <content>
    > >>> ......
    > >>> <content>
    > >>> End


    Maybe try:

    [sshaw@localhost ~]$ cat > wakawakawaka.txt
    Start
    weeeee
    dma
    ahhhhh
    irq
    oooohhh its dma
    End
    Start
    grrr
    dma
    End
    [sshaw@localhost ~]$ perl -lne'print if !/(End|Start)/'
    wakawakawaka.txt
    weeeee
    dma
    ahhhhh
    irq
    oooohhh its dma
    grrr
    dma

    > > And I need to print out both all the contents (both lines w and w/o
    > > keyword dma) for the block where there are <content_with_keyword_dma>
    > > there ?


    For blocks with/without dma, in either case you will just print, so
    why make the distinction?
     
    Skye Shaw!@#$, May 31, 2007
    #4
  5. samuel

    samuel Guest

    On 5ÔÂ31ÈÕ, ÏÂÎç9ʱ31·Ö, Brad Baxter <> wrote:
    > On May 31, 9:25 am, samuel <> wrote:
    >
    > > But How to do if the block is like below :

    >
    > > Start
    > > <content_without_keyword_dma>
    > > ......
    > > <content_with_keyword_dma>
    > > ......
    > > <content_without_keyword_dma>
    > > End

    >
    > > And I need to print out both all the contents (both lines w and w/o
    > > keyword dma) for the block where there are <content_with_keyword_dma>
    > > there ?

    >
    > my $x = do { local $/; <DATA> };
    > while ( $x =~ /^Start(.*?)^End/smg ) {
    > defined and /dma/ and print for my $y = $1;}
    >
    > __DATA__
    > Start
    > cpu
    > End
    > Start
    > dma
    > End
    > Start
    > <content_without_keyword_dma>
    > ......
    > <content_with_keyword_dma>
    > ......
    > <content_without_keyword_dma>
    > End
    >
    > --
    > Brad


    Thanks Brad,

    The script did work for me.

    But how to do if I need to remove all the blocks containing the
    keyword dma from the file , but not print them out ?

    Samuel
     
    samuel, Jun 1, 2007
    #5
  6. samuel

    samuel Guest

    On 6ÔÂ1ÈÕ, ÉÏÎç12ʱ36·Ö, "Skye Shaw!@#$" <> wrote:
    > On May 31, 7:26 am, Aukjan van Belkum <>
    > wrote:
    >
    > > samuel wrote:
    > > > On May 31, 5:40 pm, Aukjan van Belkum <>
    > > > wrote:
    > > >> samuel wrote:
    > > >>> Hi All,
    > > >>> Now I need to analyze a file which is composed of several blocks ,
    > > >>> which is defined as below :
    > > >>> Start
    > > >>> <content>
    > > >>> <content>
    > > >>> ......
    > > >>> <content>
    > > >>> End

    >
    > Maybe try:
    >
    > [sshaw@localhost ~]$ cat > wakawakawaka.txt
    > Start
    > weeeee
    > dma
    > ahhhhh
    > irq
    > oooohhh its dma
    > End
    > Start
    > grrr
    > dma
    > End
    > [sshaw@localhost ~]$ perl -lne'print if !/(End|Start)/'
    > wakawakawaka.txt
    > weeeee
    > dma
    > ahhhhh
    > irq
    > oooohhh its dma
    > grrr
    > dma
    >
    > > > And I need to print out both all the contents (both lines w and w/o
    > > > keyword dma) for the block where there are <content_with_keyword_dma>
    > > > there ?

    >
    > For blocks with/without dma, in either case you will just print, so
    > why make the distinction?


    I want to print out all the contents(the lines with or w/o dma) of the
    blocks containing the keyword dma. But for the blocks not including
    dma at all, not print out.

    So this is the distinction.
     
    samuel, Jun 1, 2007
    #6
  7. samuel

    Brad Baxter Guest

    On Jun 1, 1:20 am, samuel <> wrote:
    > On 5月31æ—¥, 下åˆ9æ—¶31分, Brad Baxter <> wrote:
    >
    >
    >
    > > On May 31, 9:25 am, samuel <> wrote:

    >
    > > > But How to do if the block is like below :

    >
    > > > Start
    > > > <content_without_keyword_dma>
    > > > ......
    > > > <content_with_keyword_dma>
    > > > ......
    > > > <content_without_keyword_dma>
    > > > End

    >
    > > > And I need to print out both all the contents (both lines w and w/o
    > > > keyword dma) for the block where there are <content_with_keyword_dma>
    > > > there ?

    >
    > > my $x = do { local $/; <DATA> };
    > > while ( $x =~ /^Start(.*?)^End/smg ) {
    > > defined and /dma/ and print for my $y = $1;}

    >
    > > __DATA__
    > > Start
    > > cpu
    > > End
    > > Start
    > > dma
    > > End
    > > Start
    > > <content_without_keyword_dma>
    > > ......
    > > <content_with_keyword_dma>
    > > ......
    > > <content_without_keyword_dma>
    > > End

    >
    > > --
    > > Brad

    >
    > Thanks Brad,
    >
    > The script did work for me.
    >
    > But how to do if I need to remove all the blocks containing the
    > keyword dma from the file , but not print them out ?
    >
    > Samuel


    Well, if it were me, I'd print out all the other ones, and save
    that. Or maybe try using Tie::File to treat the file as an
    array and splice the offending lines out.

    --
    Brad
     
    Brad Baxter, Jun 1, 2007
    #7
  8. samuel

    samuel Guest

    On 6 2 , 12 27 , Jim Gibson <> wrote:
    > In article <>,
    >
    > samuel <> wrote:
    >
    > > I want to print out all the contents(the lines with or w/o dma) of the
    > > blocks containing the keyword dma. But for the blocks not including
    > > dma at all, not print out.

    >
    > > So this is the distinction.

    >
    > Your requirements keep changing. Please read the guidelines for this
    > newsgroup. It is time you wrote a short, complete Perl program that
    > attempts to do what you want. It probably shouldn't be a one-liner.
    > Include some test data (see the guidelines on how to use the <DATA>
    > file handle for including test data in your program.) People should be
    > able to cut-and-paste your program and run it on their own systems. If
    > you do that, someone will surely help you.
    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com


    Jim,

    Thanks for the reminding.
    I will read the guideline carefully next time before posting.

    Brad/Aukjan/Skye,

    Thanks for the help.

    Samuel
     
    samuel, Jun 4, 2007
    #8
    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. John Black

    match pattern of multiple lines

    John Black, Jul 15, 2004, in forum: Perl
    Replies:
    2
    Views:
    545
    Mohammad Mahmoud Khajah
    Jul 16, 2004
  2. Markus Fischer
    Replies:
    9
    Views:
    179
    7stud --
    Apr 8, 2011
  3. Chris

    Pattern match problem

    Chris, Jan 14, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    109
    Chris
    Jan 14, 2004
  4. Chris L.

    Multiple Line Pattern Match

    Chris L., Apr 9, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    140
    Tad McClellan
    Apr 10, 2006
  5. Match line by line

    , Jun 5, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    94
Loading...

Share This Page