shweta.mehta20 has asked for the wisdom of the Perl Monks concerning the following question:

Please tell me the code in perl..... i have a file in which different blocks are there like 111\n 222\n 333\n 444\n 555\n 666\n 777\n aaa\n bbb\n ccc\n ddd\n xxx\n yyy\n zzz\n now i have to search for a string say "ccc" and have to remove the entire block so that output of the file should be 111\n 222\n 333\n 444\n 555\n 666\n 777\n xxx\n yyy\n zzz\n Can we modify the same file or we have to create a new file...?
  • Comment on find a string and delete the lines associated with that..

Replies are listed 'Best First'.
Re: find a string and delete the lines associated with that..
by ashish.kvarma (Monk) on Oct 25, 2011 at 08:20 UTC

    You can do both, perl will allow you to modify existing file or create new one. It will depend on your requirements. I would suggest you to do following:

    1. Understand the requirement before thinking on the implementation. In your case one angle of requirement is to find and remove the given block/string but what to do next is missing.
    2. Once you know what is required, try yourself, at-least give it a shot. If you still are not able to solve your issue, come to the monks with what you have tried. I am sure you will get good response and solutions.
    3. Before posting the question have look on how to format and post your question. At-least preview it before posting. Bad formatting will make it difficult to comprehend.

    Update:Corrected some typo.
    Regards,
    Ashish
Re: find a string and delete the lines associated with that..
by Anonymous Monk on Oct 25, 2011 at 08:05 UTC
    perlintro perl -ne " print unless /blah/ " < infile > outfile