Here's a technique I use: open the original file and a new file (the latter for writing). loop through the original file. If the incoming line matches the pattern you're looking for, set a flag to true. Print the incoming line to the outgoing file unless the flag is set. Then, check to see if the line matches the pattern that ends the block you're cutting out. If so, set the flag back to false. If you don't have an explicit ending line (e.g. here you've got a nice XMLish closing tag, but you might have a file like:

Server a.b.c.d
 line 1
 line 2
Server e.f.g.h
 line 1

You can monkey with the inner logic of the loop to deal with such situations, but that's left as an exercise for the reader =)

open OLD, $oldfile or die; open NEW, "> $oldfile.new" or die; my $flag = 0; while (<OLD>) { $flag =1 if /pattern/; print NEW unless $flag; $flag = 0 if /end_pattern/; } rename ("$oldfile", "$oldfile.old") or die; rename ("$oldfile.new", $oldfile) or die;

Of course, you'd replace my dies with more helpful messages, and substitute real patterns for mine.

HTH

Philosophy can be made out of anything. Or less -- Jerry A. Fodor


In reply to Re: Parsing file and removing a section by arturo
in thread Parsing file and removing a section by brendonc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.