You can use:

if (/PATTERN1/ .. /PATTERN2/) {
  # do whatever
  }    
The if condition is satisfied starting with line matching PATTERN1 and
ending with line matching PATTERN2.  It matches again on the next PATTERN1,
etc.


Assuming you have the file opened already...

while (<FILE>) {
  if (/<VirtualHost\s+(\S+)>/ .. /<\/VirtualHost>/) {
    # remember the argument to <VirtualHost> if you need it
    $host = $1 if $1 ne "";
    print "Host $host: $_";
    }
  }

If your config file had:
-----
Unrelated stuff 1
<VirtualHost host.some_domain.com>
Stuff for host.some_domain.com
</VirtualHost>
Unrelated stuff 2
<VirtualHost 192.168.1.99>
Stuff for another host
</VirtualHost>
Unrelated stuff 3
-----
The above code would output
-----
Host host.some_domain.com: <VirtualHost host.some_domain.com>
Host host.some_domain.com: Stuff for host.some_domain.com
Host host.some_domain.com: </VirtualHost>
Host 192.168.1.99: <VirtualHost 192.168.1.99>
Host 192.168.1.99: Stuff for another host
Host 192.168.1.99: </VirtualHost>
-----

The "print" was just to show you that it was processing the right
stuff and remembered the argument properly.

      pete


        pete peterson
        GenRad, Inc.
        7 Technology Park Drive
        Westford, MA 01886-0033

        [EMAIL PROTECTED] or [EMAIL PROTECTED]
        +1-978-589-7478 (GenRad);  +1-978-256-5829 (Home: Chelmsford, MA)
        +1-978-589-2088 (Closest FAX); +1-978-589-7007 (Main GenRad FAX)
 

> Date: Tue, 22 Feb 2000 16:05:49 -0600
> From: Matt Housh <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Subject: [OT] - Parsing Apache Configuration File with PERL
> Message-id: <[EMAIL PROTECTED]>
> Content-type: text/plain; charset=us-ascii
> Content-transfer-encoding: 7BIT
> 
> Does anyone know how (or could anyone point me to a good reference to
> LEARN how) to perform a parsing sequence with PERL that would be useful
> for listing configured Virtual Hosts in an Apache web server config
> file? The format of the VirtualHost directives, for anyone who has not
> used them, is this:
> 
> <VirtualHost ip>
> Configuration Directives here
> </VirtualHost>
> 
>       I'd like to create a perl script that will parse the file, IGNORING
> anything outside the <VirtualHost ip></VirtualHost> tags, but do
> something useful with the data inside them. I already know how to do the
> latter part, but how can I create a loop that will ignore everything
> until it hits <VirtualHost ip>, parse everything until it hits
> </VirtualHost>, and start ignoring again?
> 
>       Any assistance appreciated...
> 
> Matt
> 
>   ------------------------------------------------------------------
>             Matt Housh            Morpheus.Net Administrator
>     email: ([EMAIL PROTECTED]) web: (http://jaeger.morpheus.net/)
>   ------------------------------------------------------------------


-- 
To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe"
as the Subject.

Reply via email to