|
|
 |
 |
 |
 |
Perl Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
Multiple Line Pattern Match problem
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 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.
On May 31, 5:40 pm, Aukjan van Belkum <auk@vanbelkum.no.spam.nl> 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
On May 31, 9:25 am, samuel <samuelz@gmail.com> 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
samuel wrote: > On May 31, 5:40 pm, Aukjan van Belkum <auk @vanbelkum.no.spam.nl> > 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 ?
So you have: Start bla bla dma bla bla End Start bla bla bla bla End etc ... You could catch all data of for the block, and check on the 'End' if you want to keep the block e.g. while ( <IN> ){ my @block; my $dma = 0; if ( /^Start/ .. /^End/ ){ push @block, $_; $dma = 1 if m/dma/; if ( /^End/ ){ print @block if $dma; $dma = 0; } } }
Aukjan
On May 31, 7:26 am, Aukjan van Belkum <auk@vanbelkum.no.spam.nl> wrote: > samuel wrote: > > On May 31, 5:40 pm, Aukjan van Belkum <auk @vanbelkum.no.spam.nl> > > 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?
On 5 31 , 9 31 , Brad Baxter <baxter.b@gmail.com> wrote:
> On May 31, 9:25 am, samuel <samuelz @gmail.com> 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
On 6 1 , 12 36 , "Skye Shaw!@#$" <skye.s@gmail.com> wrote:
> On May 31, 7:26 am, Aukjan van Belkum <auk @vanbelkum.no.spam.nl> > wrote: > > samuel wrote: > > > On May 31, 5:40 pm, Aukjan van Belkum <auk@vanbelkum.no.spam.nl> > > > 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.
On Jun 1, 1:20 am, samuel <samuelz@gmail.com> wrote:
> On 5 31 , 9 31 , Brad Baxter <baxter.b @gmail.com> wrote: > > On May 31, 9:25 am, samuel <samuelz@gmail.com> 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
In article <1180675413.674067.327@r19g2000prf.googlegroups.com>, samuel <samuelz @gmail.com> 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
On 6 2 , 12 27 , Jim Gibson <jgib@mail.arc.nasa.gov> wrote:
> In article <1180675413.674067.327 @r19g2000prf.googlegroups.com>, > samuel <samuelz@gmail.com> 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
|
 |
 |
 |
 |
|