A quick perl hack that opens a file, looks for the word "hello" and prints
the next two lines and the two previous ones. I just put this together real
quick here at work so I know it could be prettier but use it as you will.

Mike

#!/usr/bin/perl

open (FILE, "test.txt")||die "Can't open file\n";
while (<FILE>){
     push (@lines, $_);
}
$i=0;
$found=0;
foreach $line(@lines){
     if (($found == 1) && ($count <= 1)){
          print $line;
          $count++;
          if ($count == 2){
               $found=0;
               $count=0;
          }
     }
     if ($line =~ /hello/){
          print "$lines[$i-2]";
          print "$lines[$i-1]";
          $found = 1;
          $count=0;
     }
$i++;
}


Eric Clover <[EMAIL PROTECTED]>@redhat.com on 11/16/2000 09:10:57 AM

Please respond to [EMAIL PROTECTED]

Sent by:  [EMAIL PROTECTED]


To:   redhat <[EMAIL PROTECTED]>
cc:
Subject:  cat <file>|grep <word>|??? <display 5lines before and after the
      grep>


hello,
i have a need for something that will cat a large file, grep it for a
word and have the ability to pull the 5 lines before and after (more
like 3 lines before and 8 lines after) the grep has found the word and
display the results. example:

cat <file>|grep 5|??? 2
<file>
1
2
3
4
5
6
7
8
9
10
</file>

output = 3
         4
         5 <-- being the greped word and pulling the 2 lines before and
after
         6
         7

can this be done??

thank you

eric



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list






_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to