Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Perl script to comment out lines in named.con file

by shmem (Chancellor)
on Oct 04, 2007 at 22:55 UTC ( #642783=note: print w/replies, xml ) Need Help??


in reply to Perl script to comment out lines in named.conf file

Good start, but are all your zone definitions that uniform?

You might have some that read

zone "10.in-addr.arpa" { type forward; forwarders { 10.1.2.3; 10.1.2.4; 10.1.2.5; }; };

There are, as always, many ways to do it - one:

my $comment = 0; my $block = 0; while (<HAN>) { /^zone\s+"$targetdomain"/ and $comment++; if($comment) { $block += () = /(\{)/g; # add the number of { matches t +o $block s!^!// ! if $comment && $block; $block -= () = /(\})/g; # substract the number of } mat +ches $comment = 0 unless $block; } print; }

That will work only if the opening curly after a zone declaration is on the same line.

BTW, you need a semicolon after every closing curly in your zone file ( }; ).

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Perl script to comment out lines in named.con file
by firewall00 (Acolyte) on Oct 05, 2007 at 00:20 UTC
    thanks for you replay :)
    but i want to know what are the function of two variables $comment ,, $block
    please explain me carefully cuz iam newbie :) .
    and if i want to instead of printing the commented zone in the shell
    i want how to make that inside the file affecting the matched zone only
    and iam thankful for you
      • $comment is a switch, which is turned on when the sought zone is found, and turned of if the associated block is processed. If $comment is true (i.e. not '0' and not ''), the current line is commented out.
      • $block is a counter for the curly bracket level. It is increased with each opening bracket, and decreased with each closed bracket. If the last closing bracket of the zone block is found, it is zero, we are done and can unset $comment.

      The empty () in the assignments force list context on the right hand side (global match), so we get all matches; the left hand side is a scalar ($block), so we get the count of those matches: a list evaluated in scalar context returns the number of elements of the list.

      To edit the file, it makes sense to backup the file and write it:

      #!/usr/bin/perl -w use strict; print " please enter the domain name: "; my $targetdomain = <STDIN>; chomp $targetdomain; my $file = "/home/blackice/hello"; rename $file, "$file.bak" or die "Can't rename file '$file': $!\n"; open my $in, '<', "$file.bak" or die "Can't read file '$file': $!\n"; open my $out, '>', $file or die "Can't write file '$file': $!\n"; my $comment = 0; my $block = 0; while(<$in>) { if (/^zone\s+"$targetdomain"/) { $comment++; $block += () = /(\{)/g; print $out '// '.$_; next; } if($comment) { $block += () = /(\{)/g; s!^!// ! if $comment or $block; $block -= () = /(\})/g; $comment = 0 unless $block; } print $out $_; }

      This version works also with zone entries where the opening curly is on the next line:

      zone "foo" { type "master"; ... };

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://642783]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2021-12-06 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (32 votes). Check out past polls.

    Notices?