Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Search and delete

by pr33 (Scribe)
on Dec 15, 2014 at 23:11 UTC ( [id://1110419]=perlquestion: print w/replies, xml ) Need Help??

pr33 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I want to delete a line containing a pattern and 2 lines after that pattern as well . Ex : The pattern should match unassigned and delete the line containing unassigned next 2 lines after it . Please help me with this .
unassigned physicaldrive 7C:3:7 port 7C:box 3:bay 7 SAS 1 TB OK physicaldrive 7C:3:8 port 7C:box 3:bay 8 SAS 1 TB OK
Thanks

Replies are listed 'Best First'.
Re: Search and delete
by blindluke (Hermit) on Dec 15, 2014 at 23:36 UTC

    Welcome to the Monastery.

    Please, put the data part of your question in < code > tags, it's impossible to see the lines you are referring to. Look at the Writeup Formatting Tips to find out how to format your data and code.

    Have you already tried something to match those lines? If yes, what went wrong?

    - Luke

      Hi, Thanks, for getting back to me . This are the lines that I wanted to delete . This is what I have tried after opening the File descriptor from my command output . next if $row =~ /^unassigned\n\nOK$/; Below are the lines to be deleted < unassigned physicaldrive 7C:3:7 port 7C:box 3:bay 7 SAS 1 TB OK physicaldrive 7C:3:8 port 7C:box 3:bay 8 SAS 1 TB OK >

        Have you read my response? Please apply some basic formatting to your post, it's completely unreadable right now.

        Can you see four lines in the following output?

        asdfghjklqwertyuiop

        And now?

        asd fghjkl qwertyu iop

        You can always count on people willing to help you in the Monastery. Make it possible for them by not forcing them to guess what your question is.

        - Luke

        Um, thats not code tags :)  <code> code here </code>
        unassigned -> line 1 to be matched physicaldrive......OK -> line2 physicaldrive ...Ok -> line3
Re: Search and delete
by ww (Archbishop) on Dec 15, 2014 at 23:57 UTC

    Please, READ the directions on formatting: they are prominently spelled out at the text_entry box where you created this gibberish. Alternately, READ and FOLLOW the guidance in Markup in the Monastery .

    Without proper formatting -- ie, <c> an opening code tag and </c> an end code tag -- we cannot tell what you're asking, far less offer good help.



    check Ln42!

Re: Search and delete
by 2teez (Vicar) on Dec 15, 2014 at 23:39 UTC

    Hi,
    Please post what you have done and where you are having issues with your code.
    Also check this How do I post a question effectively?

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Search and delete
by Anonymous Monk on Dec 16, 2014 at 00:32 UTC

    One problem with your program is  /^unassigned\n\nOK$/ it can never match, you're reading line byline .. you're not reading two three lines at a time

    Solution is simple, set a flag/counter of how many lines to skip

      Thanks for the prompt reply . That works for me. Regards, Praveen
Re: Search and delete
by CountZero (Bishop) on Dec 16, 2014 at 07:18 UTC
    It is not more difficult than this:
    use Modern::Perl qw '2014'; while (<DATA>) { do { print; next } unless /^unassigned$/; <DATA>; <DATA>; } __DATA__ array A physicaldrive 2C:1:1 port 2C:box 1:bay 1 SAS 1 TB OK physicaldrive 2C:1:2 port 2C:box 1:bay 2 SAS 1 TB OK array B physicaldrive 2C:1:3 port 2C:box 1:bay 3 SAS 1 TB OK physicaldrive 2C:1:4 port 2C:box 1:bay 4 SAS 1 TB OK array C physicaldrive 3C:1:5 port 3C:box 1:bay 5 SAS 1 TB OK physicaldrive 3C:1:6 port 3C:box 1:bay 6 SAS 1 TB OK array D physicaldrive 3C:1:7 port 3C:box 1:bay 7 SAS 1 TB OK physicaldrive 3C:1:8 port 3C:box 1:bay 8 SAS 1 TB OK array E physicaldrive 4C:2:1 port 4C:box 2:bay 1 SAS 1 TB OK physicaldrive 4C:2:2 port 4C:box 2:bay 2 SAS 1 TB OK array F physicaldrive 4C:2:3 port 4C:box 2:bay 3 SAS 1 TB OK physicaldrive 4C:2:4 port 4C:box 2:bay 4 SAS 1 TB OK array G physicaldrive 5C:2:5 port 5C:box 2:bay 5 SAS 1 TB OK physicaldrive 5C:2:6 port 5C:box 2:bay 6 SAS 1 TB OK array H physicaldrive 5C:2:7 port 5C:box 2:bay 7 SAS 1 TB OK physicaldrive 5C:2:8 port 5C:box 2:bay 8 SAS 1 TB OK array I physicaldrive 6C:3:1 port 6C:box 3:bay 1 SAS 1 TB OK physicaldrive 6C:3:2 port 6C:box 3:bay 2 SAS 1 TB OK array J physicaldrive 6C:3:3 port 6C:box 3:bay 3 SAS 1 TB OK physicaldrive 6C:3:4 port 6C:box 3:bay 4 SAS 1 TB OK array K physicaldrive 7C:3:5 port 7C:box 3:bay 5 SAS 1 TB OK physicaldrive 7C:3:6 port 7C:box 3:bay 6 SAS 1 TB OK unassigned physicaldrive 7C:3:7 port 7C:box 3:bay 7 SAS 1 TB OK physicaldrive 7C:3:8 port 7C:box 3:bay 8 SAS 1 TB OK

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Search and delete
by Anonymous Monk on Dec 15, 2014 at 23:42 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1110419]
Approved by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found