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
Solved

script parse on values issue

Posted on 2011-03-03
13
288 Views
Last Modified: 2012-05-11
HI Guys,

I must be a dunce :)

I am trying to modify a script from a previous thread that Wilcoxon created.

The match I am trying to get are:

zone "samaritan.local" in {
        type slave;
        file "sec_qi/db.samaritan.local";
        masters { 10.100.20.175; 10.100.20.180; };
        allow-query { Any; };
        allow-transfer { "blesstransfers"; };
        notify no;
};

zone "_msdcs.samaritan.local" in {
        type slave;
        file "sec_qi/db._msdcs.samari.local";
        masters { 10.100.20.175; 10.100.20.180;};
        check-names ignore;
        allow-query { any; };
        allow-transfer { 128.163.97.5;128.163.3.10;128.163.1.6; };
        notify no;
};

I would like it one per line in this format please.

zone samaritan.local allow-transfer="[blesstransfers]"
zone _msdcs.samaritan.local allow-transfer="[128.163.97.5;128.163.3.10;128.163.1.6]"

Code is on next thread..

0
Comment
Question by:richsark
13 Comments
 
LVL 1

Author Comment

by:richsark
ID: 35032464
Here is the code I was messing with. But I am missing something
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update,$allow-transfer $);

while (<>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone,$allow_update,$allow-transfer) {
            $allow-update =~ s{;\s*$}{};
            $allow-transfer =~ s{;\s*$}{};
            print "$zone allow_update=\"[$allow_update $allow-transfer]\"\n"
        }
        $zone = "zone $1";
        $allow-update = '';
        $allow-transfer = '';
    }
    if (/^\s*allow-update\s*{\s*\w+/../}/) {
        if (/^\s*allow-update\s*{\s*(\S+)/) {
            $allow_update = $1;
        } elsif (not /\bkey\b/ and /^\s*([^;]+;)/) {
            $allow_update .= " $1";
        }
    }
}
if ($zone,$allow_update,$allow-transfer ) {
    $allow_update =~ s{;\s*$}{};
    print "$zone allow_update=\"[$allow_update]\"\n[$allow_transfer]\"\n"
}

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 35033043
Just a gingal ..
0
 
LVL 1

Author Comment

by:richsark
ID: 35033215
This is what I am seeing

$ perl chomp.pl ncc.conf.original

Can't use global $) in "my" at chomp.pl line 6, near "transfer $)"
syntax error at chomp.pl line 6, near "$);"
Global symbol "$allow_transfer" requires explicit package name at chomp.pl line 30.
Bareword "transfer" not allowed while "strict subs" in use at chomp.pl line 11.
Bareword "update" not allowed while "strict subs" in use at chomp.pl line 12.
Bareword "transfer" not allowed while "strict subs" in use at chomp.pl line 13.
Bareword "update" not allowed while "strict subs" in use at chomp.pl line 17.
Bareword "transfer" not allowed while "strict subs" in use at chomp.pl line 18.
Bareword "transfer" not allowed while "strict subs" in use at chomp.pl line 28.
Execution of chomp.pl aborted due to compilation errors.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:richsark
ID: 35033432
Hi Guys,

I am trying to solve this, but again I am not doing it right.

Please help
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update $allow-transfer $allow_transfer $);

while (<>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone and $allow_update $allow_transfer) {
            $allow_update =~ s{;\s*$}{};
            print "$zone allow_update=\"[$allow_update]\"\n"
        }
        $zone = "zone $1";
        $allow_update = '';
    }
    if (/^\s*allow-update\s*{\s*\w+/../}/) {
        if (/^\s*allow-update\s*{\s*(\S+)/) {
            $allow_update = $1;
            $allow_update = '' if ($allow_update eq 'key');
        } elsif (not /\bkey\b/ and /^\s*([^;]+;)/) {
            $allow_update .= ($allow_update ? ' ' : '') . $1;
        }
    }
}
if ($zone and $allow_update) {
    $allow_update =~ s{;\s*$}{};
    print "$zone allow_update=\"[$allow_update]\"\n";
}

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 35035821
Hi, did I do something wrong that I yet to get help on ?

Rich
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35036006
Does it have to be Perl? How about awk, tr, sed?

#!/bin/sh
inputfile=/path/to/inputfile
awk  '/^zone/           {printf $1 " " $2 " "};
      /allow-transfer/  {printf $1 "=#[" $2 $3 "]#\n"}' $inputfile \
      | tr -d "{\"" \
      | tr "#" "\"" \
      | sed "s/;]/]/g"
exit

Open in new window


wmp
0
 
LVL 1

Author Comment

by:richsark
ID: 35036291
sure, that would be fine,

I ran it, looks good, but only the first line is appended with a few zones

zone ikuu8z.com zone ikuu8q.com zone molendf.kr zone cs.engr.uky.edu allow-transfer="[blesstransfers]"
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35036329
Seems that your sample is not representative for what you're actually going to process!

Obviously there are "zone" entries without a following "allow-transfer"!
0
 
LVL 1

Author Comment

by:richsark
ID: 35036803
right, I do not want it to report if there are no allow-transfer filled in.

I am also trying to get allow-update stuff too. so if its filled in as "None" for either, then dont report it.

Does that make sense?
0
 
LVL 1

Author Comment

by:richsark
ID: 35037815
HI,

all I need is to have an output like so for both allow_transfer and allow_update


zone modify _msdcs.as.foo.edu set allow_transfer="[128.163/16]"
zone modify _msdcs.as.foo.edu set allow_update="[128.163.119.96,128.163.119.95,128.163.119.94,128.163.119.93;128.163.119.103]"

I tried playing with it, but I cant get it. please help
#!/bin/sh
inputfile=ncc.conf.original
awk  '/^zone/           {printf $1 " " $2 " "};
      /allow-transfer/  {printf $1 "=#[" $2 $3 "]#\n"}' $inputfile \
      /allow-update/  {printf $1 "=#[" $2 $3 "]#\n"}' $inputfile \
      | tr -d "{\"" \
      | tr "#" "\"" \
      | sed "s/;]/]/g"
exit

Open in new window

0
 
LVL 22

Expert Comment

by:blu
ID: 35038779
Back to your error messages. The problem you are showing is that you used dashes "-" instead of underscores "_" in the variable names.
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 35038884
This should do what you want...

Again, part of your problem is that $allow-transfer is not a legal variable name ($allow_transfer is - underscore is legal but dash is not).
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update, $allow_xfer);

while (<DATA>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone and $allow_update) {
            $allow_update =~ s{;\s*$}{};
            print "$zone allow_update=\"[$allow_update]\"\n"
        }
        if ($zone and $allow_xfer) {
            $allow_xfer =~ s{;\s*$}{};
            $allow_xfer =~ s{;\s*}{, }g;
            print "$zone allow_transfer=\"[$allow_xfer]\"\n";
        }
        $zone = "zone $1";
        $allow_update = '';
        $allow_xfer = '';
    }
    if (/^\s*allow-update\s*{\s*\w+/../}/) {
        if (/^\s*allow-update\s*{\s*(\S+)/) {
            $allow_update = $1;
            $allow_update = '' if ($allow_update eq 'key');
        } elsif (not /\bkey\b/ and /^\s*([^;]+;)/) {
            $allow_update .= ($allow_update ? ' ' : '') . $1;
        }
    }
    if (/^\s*allow-transfer\s*{\s*\S+/../}/) {
        if (/^\s*allow-transfer\s*{\s*(\S+)/) {
            $allow_xfer = $1;
            $allow_xfer =~ s{"}{}g;
        }
    }
}
if ($zone and $allow_update) {
    $allow_update =~ s{;\s*$}{};
    print "$zone allow_update=\"[$allow_update]\"\n";
}
if ($zone and $allow_xfer) {
    $allow_xfer =~ s{;\s*$}{};
    $allow_xfer =~ s{;\s*}{, }g;
    print "$zone allow_transfer=\"[$allow_xfer]\"\n";
}

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 35039107
Oh... Thanks wilcoxon, Let me look at your code to understand. Thank you kindly

Rich
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

856 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