Thank you all..
But i dont want to use any perl modules in the code. I am trying to get a
logic without any help from the additional modules.
Thanks and Regards,
Dharshana
On 5/21/07, Mumia W. <[EMAIL PROTECTED]> wrote:
On 05/20/2007 11:37 PM, 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
>
You could use Tie::File to treat the file's lines as an array:
use strict;
use warnings;
use Fcntl 'O_RDONLY';
require 'Tie/File.pm';
tie my @file, 'Tie::File', 'anyfile.txt', mode => O_RDONLY
or die("Tie::File failed: $!");
for my $n (0 .. $#file) {
local $\ = "\n";
$_ = $file[$n];
if (/CHANNEL_INFO_T/) {
print $file[$n-2];
print $file[$n-1];
print;
}
}
untie @file;
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/