5% assure discount from here only

Monday, April 18, 2011

(26) How to print a matching line, one line immediately above it and one line immediately below?

Solution:

In perl this type of problems or requirement can be easily solved because of its versatility.

my $line='';

open(FILE,'/home/lalit/Desktop/test.txt') or die "Could not open file " . $!;

while (<FILE>){

    if($_ =~ /how are you/ig){
        print "\nprevious line of matching pattern =>" . $line;
        print "\nCurrent Line of matching pattern =>" . $_;
        print "\nNext Line of matching pattern =>" . scalar <FILE>."\n";
    }
    $line = $_;
}

close FILE;

1 comment:

  1. This is really good . i am sure this will help me a lot.. !! thanks ...Still guessing wt scalar does ther ?? Got to google it out.

    Thanks a lot :)

    ReplyDelete