Multiple Line Pattern Match problem

S

samuel

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
 
S

samuel

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
 
B

Brad Baxter

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
 
S

Skye Shaw!@#$

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

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

samuel

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

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
 
S

samuel

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

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.
 
B

Brad Baxter

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.
 
S

samuel

samuel said:
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.


----------------------------------------------------------

----------------------------------------------------------
color]

Jim,

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

Brad/Aukjan/Skye,

Thanks for the help.

Samuel
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top