parse through a conf file

richsark
richsark used Ask the Experts™
on
HI,

Below is a script that group0 helped me with.

Some how, the output is giving me too many of these

        }

    }
}

# DHCP_GROUP: SEC
{

I need to have it like so, hence remove the brackets


# DHCP_GROUP: SEC

I cant seem to figure out how to make this script do it


Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Here is the script: and the raw file.

The origianl thread is here for reference:
http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_26839982.html

Thanks
#!/usr/bin/perl

use strict;

my $block_level = 0;
my $in_group_scope = 0;
my $group_scope_block_level = 0;
my $group_option_domain;
while (<>) {
        next if (/^\s*failover peer/);
        if (/^\s*group\s*{/) {
                $in_group_scope = 1;
                $group_scope_block_level = $block_level;
                $block_level++;
                print "#$_";
                next;
        }
        if ($in_group_scope && /(option\s+domain-name\s+".*";)/) {
                $group_option_domain = $1;
                print "#$_";
                next;
        }
        if (/{\s*$/) {
                $block_level++;
        } elsif (/^\s*}/) {
                $block_level--;
                if ($in_group_scope && $group_scope_block_level == $block_level) {
                        $in_group_scope = 0;
                        $group_scope_block_level = 0;
                        $group_option_domain = '';
                        print "#$_";
                        next;
                }
        }
        print $_;
        if ($in_group_scope && /pool/) {
                print "  " x $block_level . $group_option_domain . "\n";
        }
}

Open in new window

bc-raw.txt

Commented:
It seems to be working for me.
How do you run your script?

Author

Commented:
It does, but I need to remove this from the output


        }

    }
}

# DHCP_GROUP: SEC
{

and leave only the # DHCP_GROUP: SEC in each section
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Commented:
So you don't need to "balance out" the curly braces, i.e. this

# DHCP_GROUP: SEC
group
{
    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    ddns-domainname "pcc-domain.pima.edu";
    max-lease-time 604800;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    # DHCP_SUBNET: SE-ADM1
    subnet 144.90.221.128 netmask 255.255.255.128
    {
        ddns-updates true;
        option netbios-node-type 8;
        default-lease-time 604800;
        ddns-domainname "pcc-domain.pima.edu";
        max-lease-time 604800;
        option routers 144.90.221.129;
        # DHCP_POOL: SE-VLAN20
        pool
        {

            option routers 144.90.221.129;
            range 144.90.221.141 144.90.221.240;
        }

    }
}


Should become this:

group
{
    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    ddns-domainname "pcc-domain.pima.edu";
    max-lease-time 604800;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    # DHCP_SUBNET: SE-ADM1
    subnet 144.90.221.128 netmask 255.255.255.128
    {
        ddns-updates true;
        option netbios-node-type 8;
        default-lease-time 604800;
        ddns-domainname "pcc-domain.pima.edu";
        max-lease-time 604800;
        option routers 144.90.221.129;
        # DHCP_POOL: SE-VLAN20
        pool
        {

            option routers 144.90.221.129;
            range 144.90.221.141 144.90.221.240;


If not, please provide an example.

Thanks.

Author

Commented:
Yes. Correct.

Can the attached script be modified to do this. I could not figure it out.

Commented:
Replace line 10 from
        next if (/^\s*failover peer/);
to
        next if (/^(\s*failover peer|# DHCP_GROUP|\s*})/);

and hopefully that will do the trick...

Good luck!

Author

Commented:
Ok. Let me try in the morning and let you know .

Thanks

Author

Commented:
Hi, not good yet.

Below is what it looks like from origianl script

dhcpd.conf

# DHCP_GROUP: GV
{
    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    max-lease-time 864000;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    option ntp-servers 144.90.3.1;
    # DHCP_SUBNET: GV-ADM1
    subnet 144.90.212.0 netmask 255.255.255.0
    {
        ddns-updates true;
        ping-check true;
        option netbios-node-type 8;
        default-lease-time 604800;
        ddns-domainname "pcc-domain.pima.edu";
        max-lease-time 864000;
        option routers 144.90.212.1;
        # DHCP_POOL: GV-VLAN20
        pool
        {

            range 144.90.212.56 144.90.212.240;
        }

    }
    # DHCP_SUBNET: GV-ACA
    subnet 144.90.213.0 netmask 255.255.255.0
    {
        ddns-updates true;
        ping-check true;
        option netbios-node-type 8;
        default-lease-time 86400;
        ddns-domainname "edu-domain.pima.edu";
        max-lease-time 864000;
        option routers 144.90.213.1;
        # DHCP_POOL: GV-VLAN30
        pool
        {

            range 144.90.213.56 144.90.213.240;
        }

    }
}

# DHCP_GROUP: SEC
{
    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    ddns-domainname "pcc-domain.pima.edu";
    max-lease-time 604800;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    # DHCP_SUBNET: SE-ADM1
    subnet 144.90.221.128 netmask 255.255.255.128
    {
        ddns-updates true;
        option netbios-node-type 8;
        default-lease-time 604800;
        ddns-domainname "pcc-domain.pima.edu";
        max-lease-time 604800;
        option routers 144.90.221.129;
        # DHCP_POOL: SE-VLAN20
        pool
        {

            option routers 144.90.221.129;
            range 144.90.221.141 144.90.221.240;
        }

    }
}

# DHCP_GROUP: PCAE-ES
{
    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    max-lease-time 864000;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    # DHCP_SUBNET: ES-ADM1
    subnet 144.90.232.0 netmask 255.255.255.0
    {
        ddns-updates true;
        ddns-domainname "pcc-domain.pima.edu";
        authoritative ;
        option routers 144.90.232.1;
        # DHCP_POOL: VLAN20
        pool
        {

            option routers 144.90.232.1;
            range 144.90.232.56 144.90.232.240;
        }

    }

Open in new window


This is what I need it to look like

#dhcpd.conf


# DHCP_GROUP: GV

    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    max-lease-time 864000;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    option ntp-servers 144.90.3.1;
    # DHCP_SUBNET: GV-ADM1
    subnet 144.90.212.0 netmask 255.255.255.0
    {
        ddns-updates true;
        ping-check true;
        option netbios-node-type 8;
        default-lease-time 604800;
        ddns-domainname "pcc-domain.pima.edu";
        max-lease-time 864000;
        option routers 144.90.212.1;
        # DHCP_POOL: GV-VLAN20
        pool
        {

            range 144.90.212.56 144.90.212.240;
        }

    }
    # DHCP_SUBNET: GV-ACA
    subnet 144.90.213.0 netmask 255.255.255.0
    {
        ddns-updates true;
        ping-check true;
        option netbios-node-type 8;
        default-lease-time 86400;
        ddns-domainname "edu-domain.pima.edu";
        max-lease-time 864000;
        option routers 144.90.213.1;
        # DHCP_POOL: GV-VLAN30
        pool
        {

            range 144.90.213.56 144.90.213.240;
        }

    }

# DHCP_GROUP: SEC

    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    ddns-domainname "pcc-domain.pima.edu";
    max-lease-time 604800;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    # DHCP_SUBNET: SE-ADM1
    subnet 144.90.221.128 netmask 255.255.255.128
    {
        ddns-updates true;
        option netbios-node-type 8;
        default-lease-time 604800;
        ddns-domainname "pcc-domain.pima.edu";
        max-lease-time 604800;
        option routers 144.90.221.129;
        # DHCP_POOL: SE-VLAN20
        pool
        {

            option routers 144.90.221.129;
            range 144.90.221.141 144.90.221.240;
        }

    }


# DHCP_GROUP: PCAE-ES

    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    max-lease-time 864000;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    # DHCP_SUBNET: ES-ADM1
    subnet 144.90.232.0 netmask 255.255.255.0
    {
        ddns-updates true;
        ddns-domainname "pcc-domain.pima.edu";
        authoritative ;
        option routers 144.90.232.1;
        # DHCP_POOL: VLAN20
        pool
        {

            option routers 144.90.232.1;
            range 144.90.232.56 144.90.232.240;
        }

    }

Open in new window

Commented:
Try replacing this starting at line 10:
from:
        next if (/^\s*failover peer/);
        if (/^\s*group\s*{/) {
                $in_group_scope = 1;
                $group_scope_block_level = $block_level;
                $block_level++;
                print "#$_";
                next;
        }
to:

        next if (/^(\s*failover peer|}|{)/);
        if (/^\s*group\s*{*/) {
                $in_group_scope = 1;
                $group_scope_block_level = $block_level;
                $block_level++;
#                print "#$_";
            print "\n";
                next;
        }

Author

Commented:
AWESOME !

Commented:
Glad you like.
Happy Perl'ing!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial