Hi brendonc

The script below shows one way to parse a file when looking for blocks to modify or excize. It splits the file data into an alternating sequence of block separators and block internals, using the regex that matches both separators. Then it scans the sequence backwards, looking for triplets where block-start, block-internals and block-end match your criteria and it splices out the triplet(s).

Since I don't have a httpd.conf, I made up data from your posting. As is, the script below removes the block containing ServerNameTwo.

Be warned that this use of split might be considered scary.<br

HTH
Rudif
#! perl -w use strict; $/ = undef; my $data = <DATA>; my @fields = split /(?<=\n)(<\/?VirtualHost.*>[ \t]*\n)/, $data; printf "sanity check before: %d fields\n", scalar @fields; for (my $i = $#fields-2; $i >= 0; --$i) { if ($fields[$i] =~ m(<VirtualHost.*>[ \t]*\n) && $fields[$i+1] =~ m(ServerNameTwo) && $fields[$i+2] =~ m(<\/VirtualHost>[ \t]*\n) ) { splice @fields, $i, 3; } } printf "sanity check after: %d fields\n", scalar @fields; print join '', @fields; __END__ ### mock httpd.conf file used as test data ### Hello everyone, This is my first post. I've been search through previous posts and several Perl books but I haven't been able to find a solution to my problem. I'm quite new to Perl by the way. Here's the problem. I need to parse a httpd.conf file and remove a VirtualHost block. It looks like this: <VirtualHost 123.123.123.123> ServerNameOne www.domain.com ... </VirtualHost> I need to be able to parse the file, find the appropriate ServerName, and then remove that entire VHost block. I'm stuck on this one. If anyone can help I would greatly appreciate i +t. Thanks, Brendon <VirtualHost 123.123.123.123> ServerNameTwo www.domain.com ... </VirtualHost> qwertzuiop... <SomeOtherBlock> qwertzuiop... </SomeOtherBlock> <VirtualHost 123.123.123.123> ServerNameThree www.domain.com ... </VirtualHost> qwertzuiop... qwertzuiop... qwertzuiop...

In reply to Re: Parsing file and removing a section by Rudif
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.