Hello,

     Since you are having the whole file in an array.
    1. Keep an index to specify the current line read
    2. Once you found the desired string, you have the index also. So you can
        decrement the index and get the lines.

$inex = 0;
foreach $line (@lines) {
$index++;
if($line =~ m/ CHANNEL_INFO_T/)
{
       $prevLine1 = @lines[$index-1];
        $prevLine2 = @lines[$index-2];
}
 # do something with $line
}



Hope this helps
Madan


Dharshana Eswaran wrote:
Hi All,

The below code helps in reading a file in reverse:

use strict;
use warning;

open( FILE, "<$file_to_reverse" )

 or die( "Can't open file file_to_reverse: $!" );

@lines = reverse <FILE>;
foreach $line (@lines) {
 # do something with $line
}

But i am trying to grep for a string in the file and once i get the string,

I need to read few lines which occurs before the string. For eg:

typedef union
{
   TYPE_T type;
  MODE_T mode;
} CHANNEL_INFO_T;

Here, I grep for CHANNEL_INFO_T, once i get that, i need to read the
elements defined in the union or structure.


I have written a code but i am unable to achieve what i wanted.

Can anyone guide me in this?

Thanks and Regards,
Dharshana



--
/home/madank/personal/signature.html MyselfThanx & Regards !!
from Madan
Interra Systems India Pvt. Ltd.
A10, Sec9,NOIDA Ph: 0120-2442273/4 Ext 137
             visit my homepage
           




Reply via email to