Split configuration File

Hi all,
i need to split a file containing 300 vhosts in 300 file named as the vhost
example:
Code:
<VirtualHost *>
DocumentRoot /usr/local/www/apache22/data/sito1
php_admin_value open_basedir /usr/local/www/apache22/data/sito1
php_admin_value upload_tmp_dir /usr/local/www/apache22/data/sito1/tmp
DirectoryIndex index.html
ServerName www.sito1.it
CustomLog /usr/local/www/log/sito1.access_log  combined
</VirtualHost>

<VirtualHost *>
DocumentRoot /usr/local/www/apache22/data/sito2.com
php_admin_value open_basedir /usr/local/www/apache22/data/sito2.com
php_admin_value upload_tmp_dir /usr/local/www/apache22/data/sito2.com/tmp
ServerName www.sito2.com
CustomLog /usr/local/www/log/sito2.com.access_log  combined
</VirtualHost>

needs to be split into two files:

site1.vhost
Code:
<VirtualHost *>
DocumentRoot /usr/local/www/apache22/data/sito1
php_admin_value open_basedir /usr/local/www/apache22/data/sito1
php_admin_value upload_tmp_dir /usr/local/www/apache22/data/sito1/tmp
DirectoryIndex index.html
ServerName www.sito1.it
CustomLog /usr/local/www/log/sito1.access_log  combined
</VirtualHost>
site2.vhost
Code:
<VirtualHost *>
DocumentRoot /usr/local/www/apache22/data/sito2.com
php_admin_value open_basedir /usr/local/www/apache22/data/sito2.com
php_admin_value upload_tmp_dir /usr/local/www/apache22/data/sito2.com/tmp
ServerName www.sito2.com
CustomLog /usr/local/www/log/sito2.com.access_log  combined
</VirtualHost>
I think the best practice is to use the Virtualhost /Virtualhost delimiters but i'm not so good at awk/sed/perl to do this.
Any idea?
 
Done!
Code:
<?php
$righe = file('vhost.txt');
$trovato = false;
$inizio = '<VirtualHost *>';
$name = 'ServerName';
foreach($righe as $riga) {

        if(substr($riga, 0, strlen($inizio)) == $inizio) {
                $i++;
        }
        if(trim($riga) != '') { $host[$i][] = $riga; }
}
foreach($host as $h) {
        $nome = false;
        foreach($h as $v) {
                if(substr($v, 0, strlen($name)) == $name) {
                        $nomehost = str_replace("www.", "", trim(substr($v, strlen($name))));
                        $format[$nomehost] = $h;
                        $nome = true;
                }
                if($nome) break;
        }
}

foreach($format as $nomefile => $contenuto) {

        @unlink($nomefile.'.vhost');
}
foreach($format as $nomefile => $contenuto) {

        $fp = fopen($nomefile.'.vhost', 'a');
        fwrite($fp, implode("\n", $contenuto));

        fclose($fp);
}

?>
 
Back
Top