Solved

grep help

Posted on 2011-03-02
15
358 Views
Last Modified: 2012-05-11
Hi guys,

I am trying to grep out certain items from a named.conf

};

zone "3.5.10.in-addr.arpa" {
      type master;
      file "db.10.5.3";
      allow-update { w2kdcs;
                  key dhcp-dns.foo.edu.;
                  10.8.1.231; };
};

zone "10.6.10.in-addr.arpa" {
      type master;
      file "db.10.6.10";
      allow-update { 10.8.1.231;
                  key dhcp-dns.findlay.edu.; };
};

When I run the command, it does not look right:

$ perl chomp-named.pl named.conf
-update-update-update-update-update-update-update-update-update-updatezone "0.0.127.in-addr.arpa"
-updatezone "1.10.in-addr.arpa"
-updatezone "1.10.in-addr.arpa"
-updatezone "2.10.in-addr.arpa"
-updatezone "2.10.in-addr.arpa"
-updatezone "1.3.10.in-addr.arpa"
-updatezone "1.3.10.in-addr.arpa"

I am trying to get an output like so

zone 3.5.10.in-addr.arpa allow_update="[w2kdcs]"
zone 10.6.10.in-addr.arpa allow_update="[10.8.1.231]"

Thank you



while (<>) {
        if (/zone.*{/) {
                $zone = $_;
                $zone =~ s/\{//;

        }
        if (/^.allow-update.*/) {
#              $allow-update = $_;
#                $allow-update =~ s/\{//;

        }
                if (/^.*}/) {
                print "$zone$allow-update";
        }
}

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
  • 8
  • 7
15 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35022332
*ALWAYS* use strict and warnings.  It will help you catch a lot of errors.

Part of the problem is that $allow-update is an illegal variable name.

This will work...
#!/usr/local/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
        chomp;
        if (/^\s*zone\s*"([^"]+)"/) {
                print "$zone $allow_update" if ($zone and $allow_update);
                $zone = "zone $1";

        }
        if (/^\s*allow-update\s*{\s*(\w+)/) {
              $allow_update = "allow_update=\"[$1]\"";

        }
}
print "$zone $allow_update" if ($zone and $allow_update);

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 35022523
Thanks wilcoxon!

I ran it.

I created a file called chomp.pl

executed:

perl chomp.pl named.conf > output.txt

But its all over the place.

zone 1.10.in-addr.arpa allow_update="[10]"zone 2.10.in-addr.arpa allow_update="[10]"zone 1.3.10.in-addr.arpa allow_update="[w2kdcs]"zone 2.3.10.in-addr.arpa allo

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

zone 3.5.10.in-addr.arpa allow_update="[w2kdcs]"
zone 10.6.10.in-addr.arpa allow_update="[10.8.1.231]"
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
        chomp;
        if (/^\s*zone\s*"([^"]+)"/) {
                print "$zone $allow_update" if ($zone and $
                $zone = "zone $1";

        }
        if (/^\s*allow-update\s*{\s*(\w+)/) {
              $allow_update = "allow_update=\"[$1]\"";

        }
}
print "$zone $allow_update" if ($zone and $allow_update);

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 35022653
HI,

I tried the \n and did not work


#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
        chomp;
        if (/^\s*zone\s*"([^"]+)"/) {
                print "$zone $allow_update" if ($zone and $allow_update);
                $zone = "zone $1";

        }
        if (/^\s*allow-update\s*{\s*(\w+)/) {
              $allow_update = "allow_update=\"[$1]\"";

        }
}
print "$zone $allow_update" if ($zone and $allow_update);"\n"

Open in new window

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 26

Expert Comment

by:wilcoxon
ID: 35022799
This should fix it.
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
        chomp;
        if (/^\s*zone\s*"([^"]+)"/) {
                print "$zone $allow_update\n" if ($zone and $
                $zone = "zone $1";

        }
        if (/^\s*allow-update\s*{\s*(\w+)/) {
              $allow_update = "allow_update=\"[$1]\"";

        }
}
print "$zone $allow_update\n" if ($zone and $allow_update);

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 35023743
Hi wilcoxon:

Sorry to trouble you, but the above code is giving me an error on line 12

$ perl chomp-named.pl named.conf

syntax error at chomp-named.pl line 12, near ""zone $1";"
Execution of chomp-named.pl aborted due to compilation errors.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35023930
Sorry.  Somehow, a partial line got truncated (no idea how it lost just a part of one line).
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
        chomp;
        if (/^\s*zone\s*"([^"]+)"/) {
                print "$zone $allow_update\n" if ($zone and $allow_update);
                $zone = "zone $1";

        }
        if (/^\s*allow-update\s*{\s*(\w+)/) {
              $allow_update = "allow_update=\"[$1]\"";

        }
}
print "$zone $allow_update\n" if ($zone and $allow_update);

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 35023961
Hi,

Much better, but on some I am seeing on a zpne section:

zone "foo.edu" {
        type master;
        file "db.foo.edu";

#        allow-update { w2kdcs;
#                        w2kservers;
#                        key dhcp-dns.foo.edu.;
#                        10.8.1.231; };

Output is showing

zone foo.edu allow_update="[10]"
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35023982
Ah.  That's because everything is done with global variables.  I best 10 is the allow-update for the previous zone.  If there is no allow-update what do you want to output (if anything)?

This will fix it so that the incorrect allow_update will not be output but the condition currently only outputs a zone if allow_update is populated (not sure if that's what you want or not).
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
        chomp;
        if (/^\s*zone\s*"([^"]+)"/) {
                print "$zone $allow_update\n" if ($zone and $allow_update);
                $zone = "zone $1";
                undef $allow_update;
        }
        if (/^\s*allow-update\s*{\s*(\w+)/) {
              $allow_update = "allow_update=\"[$1]\"";

        }
}
print "$zone $allow_update\n" if ($zone and $allow_update);

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 35024089
HI,

OK,
This is what I get now for some sections like so:

zone "1.10.in-addr.arpa" {
        type master;
        file "db.10.1";
        allow-update { 10.8.1.231; };
};

zone "2.10.in-addr.arpa" {
        type master;
        file "db.10.2";
        allow-update { 10.8.1.231; };
};

Output is:

zone 1.10.in-addr.arpa allow_update="[10]"
zone 2.10.in-addr.arpa allow_update="[10]"

Looks like we need to compensate for IP's as well as names

If there is no allow-update set for a zone, then I would not to report on it. Does that make sense?

Stuff like this

zone "4.5.10.in-addr.arpa" {
        type master;
        file "db.10.5.4";
        allow-update { w2kdcs;
                        key dhcp-dns.foo.edu.;
                        10.8.1.231; };
};

I would need the w2kdcs; and 10.8.1.231 and not the key dhcp-dns.findlay.edu. part please


Thanks a $$$$
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35024245
This should do it...
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone and $allow_update) {
            $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;
        } elsif (/(\d+\.\d+\.\d+\.\d+)/) {
            $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: 35024335
HI wilcoxon, Awesome, but :)...I noticed this...

for parts like these, I need to capture both lines

};

zone "1.8.10.in-addr.arpa" {
        type master;
        file "db.10.8.1";
        allow-update { w2kdcs;
                        w2kservers;
                        10.8.1.231;
                        key dhcp-dns.foo.edu.; };
};

Like so:
zone 1.8.10.in-addr.arpa allow_update="[w2kdcs;w2kservers;10.8.1.231]"

Thanks again !
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35024534
This should fix it...
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone and $allow_update) {
            $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;
        } elsif (not /\bkey\b/ and /^\s*([^;]+;)/) {
            $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: 35027465
Cool, One slight observation I see

for sections:

zone "5.252.10.in-addr.arpa" {
        type master;
        file "db.10.252.5";
        allow-update { key dhcp-dns.foo.edu.;
                        10.8.1.231; };
};

zone "6.252.10.in-addr.arpa" {
        type master;
        file "db.10.252.6";
        allow-update { key dhcp-dns.foo.edu.;
                        10.8.1.231; };
};

output looks like:

zone 5.252.10.in-addr.arpa allow_update="[key 10.8.1.231]"
zone 6.252.10.in-addr.arpa allow_update="[key 10.8.1.231]"

Thanks again for all your efforts
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 35028708
This should fix this problem.

At this point, it seems like you should be able to modify the script further to fix any other issues based on the multitude of versions I've posted above.
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone and $allow_update) {
            $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 Closing Comment

by:richsark
ID: 35028873
Thanks for baring with me and your kind help.
0

Featured Post

Technology Partners: 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

Suggested Solutions

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,…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
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…

749 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