i have a Perl Script that deleting Zones from named.conf file and here is the script
Expand|Select|Wrap|Line Numbers
- #!/usr/bin/perl -w
- use strict;
- print "please enter the domain name: ";
- chomp (my $targetdomain = <STDIN>);
- my $file = "/home/adam/Desktop/hello"; # copied to my home folder
- 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;
- next;
- }
- if($comment) {
- $block += () = /(\{)/g;
- $block -= () = /(\})/g;
- unless ($block) {
- $comment = 0;
- next;
- }
- }
- print $out $_ unless $comment;
- }
>assume this is the sample from the file
zone "foo.com" {
type xxxxx
path xxxxx
info xxxxx
};
zone "blah.com" {
type xxxxx
path xxxxx
info xxxxx
};
zone "bar.com" {
type xxxxx
path xxxxx
info xxxxx
};
suppose we enter the blah.com as the zone that will be deleted the output will be
zone "foo.com" {
type xxxxx
path xxxxx
info xxxxx
};
#2 blank lines instead of one blank line
#
zone "bar.com" {
type xxxxx
path xxxxx
info xxxxx
};
i want to keep my file order as only one line between any 2 zones .
i have tried many things : like providing a line in my code to replacing the empty line that created instead of deleted zone like
s/\n//;
but that didn't work , also i tried to set the values like $/="" to eliminate the blank line that created So Any help will be a highly appreciated and Thanks for all .