script parse on values issue

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..

LVL 1
richsarkAsked:
Who is Participating?
 
wilcoxonConnect With a Mentor Commented:
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
 
richsarkAuthor Commented:
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
 
richsarkAuthor Commented:
Just a gingal ..
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
richsarkAuthor Commented:
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
 
richsarkAuthor Commented:
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
 
richsarkAuthor Commented:
Hi, did I do something wrong that I yet to get help on ?

Rich
0
 
woolmilkporcCommented:
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
 
richsarkAuthor Commented:
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
 
woolmilkporcCommented:
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
 
richsarkAuthor Commented:
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
 
richsarkAuthor Commented:
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
 
Brian UtterbackPrinciple Software EngineerCommented:
Back to your error messages. The problem you are showing is that you used dashes "-" instead of underscores "_" in the variable names.
0
 
richsarkAuthor Commented:
Oh... Thanks wilcoxon, Let me look at your code to understand. Thank you kindly

Rich
0
All Courses

From novice to tech pro — start learning today.