469,353 Members | 2,246 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

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
Jul 19 '05 #1
6 10735
> 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";
Jul 19 '05 #2
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

Jul 19 '05 #3
On 21 Oct 2003 03:26:37 -0700, Larry Doan
<do********@hotmail.com> 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.
Jul 19 '05 #4
do********@hotmail.com (Larry Doan) wrote in message news:<70**************************@posting.google. 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.
Jul 19 '05 #5
In <comp.unix.shell> Larry Doan <do********@hotmail.com> 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, <op**********@yahoo.ca>
Linux solution for data management and processing.
Jul 19 '05 #6
William Park <op**********@yahoo.ca> wrote in message news:<7h*****************@nntp-post.primus.ca>...
In <comp.unix.shell> Larry Doan <do********@hotmail.com> 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
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Trevor Fairchild | last post: by
1 post views Thread by Nithi Gurusamy | last post: by
4 posts views Thread by Ingmar | last post: by
2 posts views Thread by Chris Lasher | last post: by
1 post views Thread by hardcoreUFO | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.