to read a line 2 lines above a line having a grepped string... - Perl

This is a discussion on to read a line 2 lines above a line having a grepped string... - Perl ; hi all, suppose i have a file having the following data.. SN = TOM FDN = SALLY OPERATIONAL STATE = ENABLED Now suppose i grep the string "ENABLED", i get the third line.But from this very line i need to ...

+ Reply to Thread
Results 1 to 5 of 5

to read a line 2 lines above a line having a grepped string...

  1. Default to read a line 2 lines above a line having a grepped string...

    hi all,

    suppose i have a file having the following data..

    SN = TOM
    FDN = SALLY
    OPERATIONAL STATE = ENABLED

    Now suppose i grep the string "ENABLED", i get the third line.But from
    this very line i need to go 2 lines above and get the SN value
    also.Please suggest .

    regards'

    Dilip


  2. Default RE: to read a line 2 lines above a line having a grepped string...


    > From: dilip [mailto:dilipkrishna82@gmail.com]
    > Sent: 11 November 2008 11:18
    > To: beginners@perl.org
    > Subject: to read a line 2 lines above a line having a grepped

    string...
    >
    > hi all,
    >
    > suppose i have a file having the following data..
    >
    > SN = TOM
    > FDN = SALLY
    > OPERATIONAL STATE = ENABLED
    >
    > Now suppose i grep the string "ENABLED", i get the third line.But from
    > this very line i need to go 2 lines above and get the SN value
    > also.Please suggest .
    >
    > regards'
    >
    > Dilip
    >

    A simple approach would be to grep for the string but return the
    line number, as opposed to the line and then seek to the line - 2.


    Information in this email including any attachments may be privileged, confidential and is intended exclusively for the addressee. The views expressed may not be official policy, but the personal views of the originator. If you have received it in error, please notify the sender by return e-mail and delete it from your system. You should not reproduce, distribute, store, retransmit, use or disclose its contents to anyone. Please note we reserve the right to monitor all e-mail communication through our internal and external networks. SKY and the SKY marks are trade marks of British Sky Broadcasting Group plc and are used under licence. British Sky Broadcasting Limited (Registration No. 2906991), Sky Interactive Limited (Registration No. 3554332), Sky-In-Home Service Limited (Registration No. 2067075) and Sky Subscribers Services Limited (Registration No. 2340150) are direct or indirect subsidiaries of British Sky Broadcasting Group plc (Registration No. 2247735). All of the companies mentioned in this paragraph are incorporated in England and Wales and share the same registered office at Grant Way, Isleworth, Middlesex TW7 5QD.

  3. Default Re: to read a line 2 lines above a line having a grepped string...

    dilip wrote:
    > hi all,


    Hello,

    > suppose i have a file having the following data..
    >
    > SN = TOM
    > FDN = SALLY
    > OPERATIONAL STATE = ENABLED
    >
    > Now suppose i grep the string "ENABLED", i get the third line.But from
    > this very line i need to go 2 lines above and get the SN value
    > also.Please suggest .


    Something like this should work:

    my @buffer;
    while ( <> ) {
    push @buffer, $_;
    shift @buffer if @buffer == 4;
    if ( /ENABLED$/ ) {
    splice @buffer, 1, 1;
    last;
    }
    }
    print @buffer;



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall

  4. Default RE: to read a line 2 lines above a line having a grepped string...

    > -----Original Message-----
    > From: John W. Krahn [mailto:jwkrahn@shaw.ca]
    > Sent: 11 November 2008 13:48
    > To: Perl Beginners
    > Subject: Re: to read a line 2 lines above a line having a grepped
    > string...
    >
    > dilip wrote:
    > > hi all,

    >
    > Hello,
    >
    > > suppose i have a file having the following data..
    > >
    > > SN = TOM
    > > FDN = SALLY
    > > OPERATIONAL STATE = ENABLED
    > >
    > > Now suppose i grep the string "ENABLED", i get the third line.But

    from
    > > this very line i need to go 2 lines above and get the SN value
    > > also.Please suggest .

    >
    > Something like this should work:
    >
    > my @buffer;
    > while ( <> ) {
    > push @buffer, $_;
    > shift @buffer if @buffer == 4;
    > if ( /ENABLED$/ ) {
    > splice @buffer, 1, 1;
    > last;
    > }
    > }
    > print @buffer;
    >
    >
    >


    Neat, thanks John

    Stu


    Information in this email including any attachments may be privileged, confidential and is intended exclusively for the addressee. The views expressed may not be official policy, but the personal views of the originator. If you have received it in error, please notify the sender by return e-mail and delete it from your system. You should not reproduce, distribute, store, retransmit, use or disclose its contents to anyone. Please note we reserve the right to monitor all e-mail communication through our internal and external networks. SKY and the SKY marks are trade marks of British Sky Broadcasting Group plc and are used under licence. British Sky Broadcasting Limited (Registration No. 2906991), Sky Interactive Limited (Registration No. 3554332), Sky-In-Home Service Limited (Registration No. 2067075) and Sky Subscribers Services Limited (Registration No. 2340150) are direct or indirect subsidiaries of British Sky Broadcasting Group plc (Registration No. 2247735). All of the companies mentioned in this paragraph are incorporated in England and Wales and share the same registered office at Grant Way, Isleworth, Middlesex TW7 5QD.

  5. Default Re: to read a line 2 lines above a line having a grepped string...

    dilip schreef:

    > suppose i have a file having the following data..
    >
    > SN = TOM
    > FDN = SALLY
    > OPERATIONAL STATE = ENABLED
    >
    > Now suppose i grep the string "ENABLED", i get the third line.But from
    > this very line i need to go 2 lines above and get the SN value
    > also.Please suggest .
    >
    > regards'


    See `man grep`, the -B option.

    --
    Affijn, Ruud

    "Gewoon is een tijger."


+ Reply to Thread