Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

re-format an output

Posted on 2011-02-28
7
Medium Priority
?
162 Views
Last Modified: 2012-06-22
Hello,

I am working with a perl script that reads a file named "static.leases.conf"

I have tried to modify the code but I cant get it do what I need. I am not sure how to move forward. I would like some help please.

The contents of the file needs to be re-complied like so. There are two types of information,
1. networks from "# 10.3.2.0/24 Network Static IP's" line in each section
2. Roaming address (that has no IP addr) but has a MAC
3  hosts with ip addresses and mac addresses, append the mac address with ':<mac>' to the ipaddress

The format of the output for 1 needs to be like:

add-networks.txt for a list of networks with mask ( I need to set the network first to import the IP's into first.

conf network add 10.3.2.0/24
conf network add 10.101.16.0/24

Format for option 3
Host-IP-MACreport.txt


conf zone foo.com add host test 10.3.2.10:00:FE:00:01:02:03
conf zone foo.com add host test 10.101.16.30:00:YT:91:04:01:20

The format, for output 2 needs to be like

report-Roaming-hosts.txt

conf network add fixed myroamer aa:bb:cc:11:22:33
conf network add fixed south-farm4-hp aa:cc:d1:13:12:73

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*Network Static IP's/);
        if (/^\s*group\s*{/) {
                $in_group_scope = 1;
                $group_scope_block_level = $block_level;
                $block_level++;
                print "#$_";
                next;
        }
        if ($in_group_scope && /(option host-name\s\hardware ethernet\s+fixed-address\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 . $in_group_scope . "\n";
        }
}

Open in new window

0
Comment
Question by:richsark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 1

Author Comment

by:richsark
ID: 34998914
Attached is the master file. dhcpd.staticleases.txt
0
 
LVL 1

Author Comment

by:richsark
ID: 35003758
Hi, just a ping to broaden the audience.

Regards
0
 
LVL 2

Expert Comment

by:clayhopkins
ID: 35028176
Hi, couple questions to clarify that you aren't shorthanding your source file.  I don't see any open statements for your input file, and all of your prints are going to the console.  Are you redirecting output to your files?

Second, the if statement that starts with if ($in_group_scope... has an extra "\" character in front of "hardware".  The combo of the "\" and the "h" will try to match horizontal white space, so that regex will never match.

Lastly, are you getting any errors when it runs, or just nothing happens?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:richsark
ID: 35028576
Hi clayhopkins:

I did not re-direct yet. I wanted to see it on screen first to make sure.

So what your saying is that I need the code to look like this?

changed this line

from

if ($in_group_scope && /(option host-name\s\hardware


to
if ($in_group_scope && /(option host-name\s hardware

Does the below look like it will work and produce me the right 3 output files?

Thanks for your attention :)

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*Network Static IP's/);
        if (/^\s*group\s*{/) {
                $in_group_scope = 1;
                $group_scope_block_level = $block_level;
                $block_level++;
                print "#$_";
                next;
        }
        if ($in_group_scope && /(option host-name\s hardware ethernet\s+fixed-address\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 . $in_group_scope . "\n";
        }
}

Open in new window

0
 
LVL 2

Accepted Solution

by:
clayhopkins earned 2000 total points
ID: 35028916
Based on the example output you specified, you don't want all of the text of the options lines, just the values.  Try the following regex for that if statement:

/[option host-name|hardware ethernet|fixed-address]\s+\"*(.*)\"*;/

Open in new window


This regex will match the three option lines, with only the values of the options placed in $1.

For example, the following line:
    option host-name "mediator"

Open in new window

would put "mediator" into $1, minus the quotes.  The pipe "|" characters in the regex make it an OR operation, and placing the quotes outside of the parens will strip them off of the host name strings.

After the regex is working you'll need to trace through and make sure your block_level flags are being incremented and decremented correctly.
0
 
LVL 1

Author Comment

by:richsark
ID: 35040799
So, clayhopkins, The formatted code would look like..
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*Network Static IP's/);
        if (/^\s*group\s*{/) {
                $in_group_scope = 1;
                $group_scope_block_level = $block_level;
                $block_level++;
                print "#$_";
                next;
        }
        if ($in_group_scope &&/[option host-name|hardware ethernet|fixed-address]\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 . $in_group_scope . "\n";
        }
}

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 35723709
Hi,

just want to confirm code on top looks correct
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Six Sigma Control Plans
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question