Get the current line and next 5 lines

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Larry Doan

    Get the current line and next 5 lines

    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
  • Andrew Shitov

    #2
    Re: Get the current line and next 5 lines

    > In a shell script or Perl, how do I open a file, find what I'm looking then[color=blue]
    >
    > Case 1: grab that line + the next 2 lines
    > Case 2: grab that line + the previous 2 lines?[/color]


    1.

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

    print "$curr$next1$ne xt2";

    2.

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

    print "$prev2$prev1$c urr";


    Comment

    • Gunnar Hjalmarsson

      #3
      Re: Get the current line and next 5 lines

      Larry Doan wrote:[color=blue]
      > 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?[/color]

      Let's see the code you have so far.

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

      Comment

      • Bill Marcum

        #4
        Re: Get the current line and next 5 lines

        On 21 Oct 2003 03:26:37 -0700, Larry Doan
        <doan_larry@hot mail.com> wrote:[color=blue]
        > 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?
        >[/color]
        If you have Gnu grep, look at the -A and -B options.

        --
        Cheops' Law:
        Nothing ever gets built on schedule or within budget.

        Comment

        • nobull@mail.com

          #5
          Re: Get the current line and next 5 lines

          doan_larry@hotm ail.com (Larry Doan) wrote in message news:<700e6ff4. 0310210226.704d a808@posting.go ogle.com>...[color=blue]
          > 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?[/color]

          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.

          Comment

          • William Park

            #6
            Re: Get the current line and next 5 lines

            In <comp.unix.shel l> Larry Doan <doan_larry@hot mail.com> wrote:[color=blue]
            > 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?[/color]

            man grep (-A -B)

            --
            William Park, Open Geometry Consulting, <opengeometry@y ahoo.ca>
            Linux solution for data management and processing.

            Comment

            • Larry Doan

              #7
              Re: Get the current line and next 5 lines

              William Park <opengeometry@y ahoo.ca> wrote in message news:<7hhlb.912 5$9K6.3582@nntp-post.primus.ca> ...[color=blue]
              > In <comp.unix.shel l> Larry Doan <doan_larry@hot mail.com> wrote:[color=green]
              > > 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?[/color]
              >
              > man grep (-A -B)[/color]


              Thanks all. I have enough info to work on.
              Larry

              Comment

              Working...