Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Get the current line and next 5 lines

Reply
Thread Tools

Get the current line and next 5 lines

 
 
Larry Doan
Guest
Posts: n/a
 
      10-21-2003
In a shell script or Perl, how do I open a file, find what I'm looking then

Case 1: grab that line + the next 2 lines
Case 2: grab that line + the previous 2 lines?

TIA,
Larry
 
Reply With Quote
 
 
 
 
Andrew Shitov
Guest
Posts: n/a
 
      10-21-2003
> In a shell script or Perl, how do I open a file, find what I'm looking then
>
> Case 1: grab that line + the next 2 lines
> Case 2: grab that line + the previous 2 lines?



1.

open FILE, "d:/1.html";
while (<FILE>){
last if /$pattern/;
}
my ($curr, $next1, $next2) = <FILE>;
close FILE;

print "$curr$next1$next2";

2.

my ($prev1, $prev2);
open FILE, "filename.txt";
while (<FILE>){
last if /$pattern/;
$prev2 = $prev1;
$prev1 = $_;
}
close FILE;
my $curr = $_;

print "$prev2$prev1$curr";


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-21-2003
Larry Doan wrote:
> In a shell script or Perl, how do I open a file, find what I'm
> looking then
>
> Case 1: grab that line + the next 2 lines
> Case 2: grab that line + the previous 2 lines?


Let's see the code you have so far.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Bill Marcum
Guest
Posts: n/a
 
      10-21-2003
On 21 Oct 2003 03:26:37 -0700, Larry Doan
<> wrote:
> In a shell script or Perl, how do I open a file, find what I'm looking then
>
> Case 1: grab that line + the next 2 lines
> Case 2: grab that line + the previous 2 lines?
>

If you have Gnu grep, look at the -A and -B options.

--
Cheops' Law:
Nothing ever gets built on schedule or within budget.
 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      10-21-2003
(Larry Doan) wrote in message news:<. com>...
> In a shell script or Perl, how do I open a file, find what I'm looking then
>
> Case 1: grab that line + the next 2 lines
> Case 2: grab that line + the previous 2 lines?


In Perl I'd use a rolling buffer to get previous lines.

my @buffer;
$#buffer = 2; # 3 line buffer (or whatever)
while (<>) {
push @buffer, $_;
shift @buffer;
next unless defined $buffer[0]; # No-op until buffer full
print @buffer if found_what_I_am_looking_for($_);
}

To get next lines you can either use the above approach (but test the
condition on $buffer[0]) or simply use a counter:

my $counter = 0;
while(<>) {
$counter = 3 if found_what_I_am_looking_for($_);
print if $counter-- > 0;
}

The newsgroup comp.lang.perl does not exist (see FAQ). Please do not
start threads here.
 
Reply With Quote
 
William Park
Guest
Posts: n/a
 
      10-21-2003
In <comp.unix.shell> Larry Doan <> wrote:
> In a shell script or Perl, how do I open a file, find what I'm looking then
>
> Case 1: grab that line + the next 2 lines
> Case 2: grab that line + the previous 2 lines?


man grep (-A -B)

--
William Park, Open Geometry Consulting, <>
Linux solution for data management and processing.
 
Reply With Quote
 
Larry Doan
Guest
Posts: n/a
 
      10-22-2003
William Park <> wrote in message news:<7hhlb.9125$>...
> In <comp.unix.shell> Larry Doan <> wrote:
> > In a shell script or Perl, how do I open a file, find what I'm looking then
> >
> > Case 1: grab that line + the next 2 lines
> > Case 2: grab that line + the previous 2 lines?

>
> man grep (-A -B)



Thanks all. I have enough info to work on.
Larry
 
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
can't get to next screen after clicking on any blue lines scd@insightbb.com Computer Support 1 09-11-2005 02:56 AM
Re: Remove a line if next line is blank Alan Connor Perl 0 05-07-2005 09:16 PM
CurrentElement->next = CurrentElement->next->next (UNDEFINED?) Deniz Bahar C Programming 2 03-09-2005 12:45 AM
next line, new line rasdj@frontiernet.net Python 3 01-31-2005 04:42 AM
Find a line, and comment out the next 5 lines joe shaboo Perl 1 04-20-2004 04:24 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57