Solved

script parse on values issue

Posted on 2011-03-03
13
290 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
[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
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
Independent Software Vendors: 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: 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

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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

691 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