Get the current line and next 5 lines

Discussion in 'Perl' started by Larry Doan, Oct 21, 2003.

  1. Larry Doan

    Larry Doan Guest

    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
     
    Larry Doan, Oct 21, 2003
    #1
    1. Advertising

  2. > 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";
     
    Andrew Shitov, Oct 21, 2003
    #2
    1. Advertising

  3. 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
     
    Gunnar Hjalmarsson, Oct 21, 2003
    #3
  4. Larry Doan

    Bill Marcum Guest

    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.
     
    Bill Marcum, Oct 21, 2003
    #4
  5. Larry Doan

    Guest

    (Larry Doan) wrote in message news:<>...
    > 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.
     
    , Oct 21, 2003
    #5
  6. Larry Doan

    William Park Guest

    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.
     
    William Park, Oct 21, 2003
    #6
  7. Larry Doan

    Larry Doan Guest

    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
     
    Larry Doan, Oct 22, 2003
    #7
    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. joe shaboo
    Replies:
    1
    Views:
    545
  2. Alan Connor
    Replies:
    0
    Views:
    3,046
    Alan Connor
    May 7, 2005
  3. next line, new line

    , Jan 31, 2005, in forum: Python
    Replies:
    3
    Views:
    370
    Jeremy Bowers
    Jan 31, 2005
  4. Jacob Rael
    Replies:
    7
    Views:
    440
    Jacob Rael
    Nov 28, 2006
  5. Deniz Bahar
    Replies:
    2
    Views:
    480
    Andrey Tarasevich
    Mar 9, 2005
Loading...

Share This Page