Solved

script parse on values issue

Posted on 2011-03-03
13
287 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux Script to remove files on specific directories with exclusions 31 99
Unix Script: Loop over all days of a month 17 96
Bulk Reorder File Names 4 67
Regex HELP 2 22
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,…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
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…

776 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