Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by shmem (Chancellor)
on Oct 05, 2007 at 07:54 UTC ( [id://642843]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Perl script to comment out lines in named.con file
in thread Perl script to comment out lines in named.conf file

  • $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://642843]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-25 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found