Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Finding consecutive lines

Reply
Thread Tools

Finding consecutive lines

 
 
Peter
Guest
Posts: n/a
 
      11-05-2004
I need to parse a file looking for patterns in 5 consecutive lines.
e.g.

I want to find 5 consecutive lines with "abc" in first line, "def" in second,
"efg" in third etc.

How can I do something like this..


Thanks in advance,
Peter
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      11-05-2004
Peter <(E-Mail Removed)> wrote:


> I want to find 5 consecutive lines with "abc" in first line, "def" in second,
> "efg" in third etc.
>
> How can I do something like this..



Buffer the 4 previous lines.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Brad Baxter
Guest
Posts: n/a
 
      11-05-2004
On Fri, 5 Nov 2004, Peter wrote:

> I need to parse a file looking for patterns in 5 consecutive lines.
> e.g.
>
> I want to find 5 consecutive lines with "abc" in first line, "def" in second,
> "efg" in third etc.
>
> How can I do something like this..
>
>
> Thanks in advance,
> Peter
>


What have you tried so far?

Regards,

Brad

#!/usr/bin/perl
use warnings;
use strict;

my @f = qw( abc def ghi jkl mno );
my @s;
while( <DATA> ) {
push @s, $_;
shift @s if @s > 5;
next unless @s == 5;
print " @s\n" if
$s[0] =~ /$f[0]/ and
$s[1] =~ /$f[1]/ and
$s[2] =~ /$f[2]/ and
$s[3] =~ /$f[3]/ and
$s[4] =~ /$f[4]/;
}
__DATA__

hey abc
there def abc
june ghi def abc
bug jkl ghi def abc
lets mno jkl ghi def abc
get mno jkl ghi def
in mno jkl ghi
the mno jkl
mud mno

__END__
hey abc
there def abc
june ghi def abc
bug jkl ghi def abc
lets mno jkl ghi def abc

there def abc
june ghi def abc
bug jkl ghi def abc
lets mno jkl ghi def abc
get mno jkl ghi def

june ghi def abc
bug jkl ghi def abc
lets mno jkl ghi def abc
get mno jkl ghi def
in mno jkl ghi

bug jkl ghi def abc
lets mno jkl ghi def abc
get mno jkl ghi def
in mno jkl ghi
the mno jkl

lets mno jkl ghi def abc
get mno jkl ghi def
in mno jkl ghi
the mno jkl
mud mno

 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      11-05-2004
Brad Baxter <(E-Mail Removed)> wrote:

> On Fri, 5 Nov 2004, Peter wrote:
>
>> I need to parse a file looking for patterns in 5 consecutive
>> lines. e.g.
>>
>> I want to find 5 consecutive lines with "abc" in first line,
>> "def" in second, "efg" in third etc.
>>

[snip]
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my @f = qw( abc def ghi jkl mno );
> my @s;
> while( <DATA> ) {
> push @s, $_;
> shift @s if @s > 5;
> next unless @s == 5;
> print " @s\n" if
> $s[0] =~ /$f[0]/ and
> $s[1] =~ /$f[1]/ and
> $s[2] =~ /$f[2]/ and
> $s[3] =~ /$f[3]/ and
> $s[4] =~ /$f[4]/;
>}


I like this a little better because it doesn't require maintenance if
the number of patterns changes.



#!/usr/bin/perl
use strict;
use warnings;

my @buffer;
my @pattern = qw(abc def ghi jkl mno);
while (<DATA>) {
push @buffer, $_;
next if @buffer != @pattern;
my $matches = grep /1/,
map $buffer[$_] =~ /$pattern[$_]/,
0 .. $#pattern;
print @buffer if $matches == @pattern;
shift @buffer;
}


__DATA__
jdfk jkdfh bl
dfjkv dfjk
dfjdj
abc these
def are
ghi the
jkl lines
mno we want
hj dfvhj d
jkfh vblsdjk
jdkf hld
 
Reply With Quote
 
ctcgag@hotmail.com
Guest
Posts: n/a
 
      11-05-2004
(E-Mail Removed) (Peter) wrote:
> I need to parse a file looking for patterns in 5 consecutive lines.
> e.g.
>
> I want to find 5 consecutive lines with "abc" in first line, "def" in
> second, "efg" in third etc.
>
> How can I do something like this..


If your file fits in memory:

warn "Untested";
$file =~ /^(.*abc.*\n.*def.*\n.*efg.*)$/m;

If course you presumably want to do something with these lines, but you
didn't say what that was.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Brad Baxter
Guest
Posts: n/a
 
      11-05-2004
On Fri, 5 Nov 2004, David K. Wall wrote:

> Brad Baxter <(E-Mail Removed)> wrote:
>
> > On Fri, 5 Nov 2004, Peter wrote:
> >
> >> I need to parse a file looking for patterns in 5 consecutive
> >> lines. e.g.
> >>
> >> I want to find 5 consecutive lines with "abc" in first line,
> >> "def" in second, "efg" in third etc.
> >>

> [snip]
> >
> > #!/usr/bin/perl
> > use warnings;
> > use strict;
> >
> > my @f = qw( abc def ghi jkl mno );
> > my @s;
> > while( <DATA> ) {
> > push @s, $_;
> > shift @s if @s > 5;
> > next unless @s == 5;
> > print " @s\n" if
> > $s[0] =~ /$f[0]/ and
> > $s[1] =~ /$f[1]/ and
> > $s[2] =~ /$f[2]/ and
> > $s[3] =~ /$f[3]/ and
> > $s[4] =~ /$f[4]/;
> >}

>
> I like this a little better because it doesn't require maintenance if
> the number of patterns changes.
>
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my @buffer;
> my @pattern = qw(abc def ghi jkl mno);
> while (<DATA>) {
> push @buffer, $_;
> next if @buffer != @pattern;
> my $matches = grep /1/,
> map $buffer[$_] =~ /$pattern[$_]/,
> 0 .. $#pattern;
> print @buffer if $matches == @pattern;
> shift @buffer;
> }
>


I agree that's better. I see you also optimized away an unnecessary
comparison.

Regards,

Brad
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      11-05-2004
On 5 Nov 2004 06:46:16 -0800, (E-Mail Removed) (Peter)
wrote:

>I want to find 5 consecutive lines with "abc" in first line, "def" in second,
>"efg" in third etc.
>
>How can I do something like this..


Slurp the whole file in and use a regex! If the whole file is huge,
then maintain a buffer and join lines five at a time as you read new
ones.


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,
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      11-06-2004
On Fri, 05 Nov 2004 17:20:46 -0000, "David K. Wall"
<(E-Mail Removed)> wrote:

>#!/usr/bin/perl
>use strict;
>use warnings;
>
>my @buffer;
>my @pattern = qw(abc def ghi jkl mno);
>while (<DATA>) {
> push @buffer, $_;
> next if @buffer != @pattern;


While your scheme is terse and elegant in that it fills in the buffer
as needed, but even if I'm not one of those paranoids about
efficiency, it bothers me a little that the check is done for all
lines, including the majority of them that would not require it. I'd
pre-load it instead.

> my $matches = grep /1/,
> map $buffer[$_] =~ /$pattern[$_]/,
> 0 .. $#pattern;


Well, I'm a big fan of map() and grep(), but in this case it seems to
me an overhead to use them. What about a simple counter instead? Also,
another source of inefficiency is in the fact that all patterns are
tried even if some of them fail, thus...

> print @buffer if $matches == @pattern;
> shift @buffer;
>}


....all in all I'd rewrite it as


#!/usr/bin/perl

use strict;
use warnings;

my @pattern = qw(abc def ghi jkl mno);
my @buffer = (0, map scalar <>, 1..$#pattern);

LINE: while (<>) {
shift @buffer; push @buffer, $_;
$buffer[$_] =~ /$pattern[$_]/ or
next LINE for 0..$#pattern;
print @buffer;
}

__END__


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,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printing two consecutive lines icebrakers2008@gmail.com Perl Misc 9 08-22-2008 04:37 PM
extract consecutive lines of data baptiste Auguié Ruby 8 11-25-2007 03:46 PM
extracting values from consecutive lines baptiste Auguié Ruby 15 10-18-2007 10:12 PM
search/replace for consecutive blank lines sbk Perl Misc 5 03-28-2005 04:31 AM
Get the same selected date from the calendar two consecutive times =?Utf-8?B?bWc=?= ASP .Net 2 06-02-2004 10:21 AM



Advertisments