in reply to Re^3: Deleting lines from named.conf file
in thread Deleting lines from named.conf file

thanks shmem but why you describe my task as trivial
i want to let you know that i have tried many times before i posted here and i search google and also used the super search provided here and i cannot solve it
so i provide a description of what is the problem and the way i thought that will solve the problem like as i mentioned before
#the first part of the code as the same last code i provided #!/usr/bin/perl -w use strict; print " please enter the domain name: "; my $targetdomain = <STDIN>; my $susdate = `date -u "+%m/%d/%Y %H:%M"`; my $char = "This Domain is Suspended at "; chomp $targetdomain; my $file = "/home/adam/Desktop/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 $block = 0; while(<$in>) { if (/^zone\s+"$targetdomain"/) { #here i want to start to deleting lines i think it will done by s// /; #by replacing the lines with an empty lines #then i need to increase the $block by 1 to add the first #open bracke +t by using $block += () = /(\{)/g; #then i want to make something like condition if the block #variable i +s exist add one also to block and then continue #deleting lines if ($block) { s// /; #or if it find a closed bracket decrease the $block by $block -= () = /(\})/g; #and if the block equal to 0 stop deleting lines #then print the output
i just want to provide some of what i think about my program
by the way the code you provide me didn't work
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 $_ unless $comment; }
i think it may need a loop to loop throw target domain zone and then delete the files from the zone header to the end of zone

Replies are listed 'Best First'.
Re^5: Deleting lines from named.conf file
by shmem (Chancellor) on Nov 10, 2007 at 21:52 UTC
    by the way the code you provide me didn't work

    Which part didn't work? did you add some print statements to your code to see what each variable holds at each state of execution?

    I suspect your main problem is

    my $targetdomain = <STDIN>;

    $targetdomain is a line from STDIN complete with the newline char. This will match your targetdomain only if it is written as

    zone "foo.bar.com " {

    and only if you read the file as one chunk and do a multiline match. So change that to

    chomp (my $targetdomain = <STDIN>);

    to remove the trailing newline.

    update: To get help without suspecting, it is best to provide a complete set of data: input, program, expected output, actual output. Do so with the snippet that "didn't work".

    --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}
      i mean didnot work didnot perform what i was expected to delete lines from file
      as you provide here :
      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 $_ unless $comment; }
      you comment print statement to deny the comments and then use print $out $_ unless $comment; which is completely logical
      but when i run it didn't delete the zone .....,
      i think i need to provide something that written inside a loop to replace the lines of the zone with space like
      s///;

      what do you think about that?
      thanks shmem for your great support
      warm regards
        i think i need to provide something that written inside a loop to replace the lines of the zone with space like
        s///;

        That statement doesn't do anything other than substituting nothing with nothing, which isn't anything you could call something.

        Sample named.conf:

        options { listen-on port 53 { 127.0.0.1; }; // listen-on-v6 port 53 { ::1; }; directory "/var/named"; dump-file "/var/named/data/cache_dump.db"; statistics-file "/var/named/data/named_stats.txt"; memstatistics-file "/var/named/data/named_mem_stats.txt"; allow-query { localhost; }; forwarders { 2.3.4.5; }; recursion yes; }; logging { channel default_debug { file "data/named.run"; severity dynamic; }; }; zone "." IN { type hint; file "named.ca"; }; zone "foo.bar.de" { type master; file "foo-bar"; allow-transfer { 1.2.3.4; 1.2.3.5; }; }; zone "quux.info" { type forward; forwarders { 3.4.5.6; 4.5.6.7; }; }; zone "blah-fasel.de" { type forward; forwarders { 5.6.7.8; }; }; zone "10.in-addr.arpa" { type forward; forwarders { 1.2.3.4; 2.3.4.5; 3.4.5.6; }; }; include "/etc/named.rfc1912.zones";

        The following script is used to delete a zone:

        #!/usr/bin/perl -w use strict; print "please enter the domain name: "; chomp (my $targetdomain = <STDIN>); my $file = "named.conf"; # 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; # print $out '// '.$_; next; } if($comment) { $block += () = /(\{)/g; s!^!// ! if $comment or $block; $block -= () = /(\})/g; unless ($block) { $comment = 0; next; } } print $out $_ unless $comment; }

        We delete the zone "quux.info" using that script, named 'zonedel.pl':

        qwurx [shmem] ~ > perl zonedel.pl please enter the domain name: quux.info

        The zone file (named.conf) is now:

        options { listen-on port 53 { 127.0.0.1; }; // listen-on-v6 port 53 { ::1; }; directory "/var/named"; dump-file "/var/named/data/cache_dump.db"; statistics-file "/var/named/data/named_stats.txt"; memstatistics-file "/var/named/data/named_mem_stats.txt"; allow-query { localhost; }; forwarders { 2.3.4.5; }; recursion yes; }; logging { channel default_debug { file "data/named.run"; severity dynamic; }; }; zone "." IN { type hint; file "named.ca"; }; zone "foo.bar.de" { type master; file "foo-bar"; allow-transfer { 1.2.3.4; 1.2.3.5; }; }; zone "blah-fasel.de" { type forward; forwarders { 5.6.7.8; }; }; zone "10.in-addr.arpa" { type forward; forwarders { 1.2.3.4; 2.3.4.5; 3.4.5.6; }; }; include "/etc/named.rfc1912.zones";

        Running 'diff -uNw named.conf named.conf.bak' shows:

        --- named.conf 2007-11-11 00:25:18.000000000 +0100 +++ named.conf.bak 2007-11-11 00:18:17.000000000 +0100 @@ -46,6 +46,13 @@ 1.2.3.5; }; }; +zone "quux.info" { + type forward; + forwarders { + 3.4.5.6; + 4.5.6.7; + }; +}; zone "blah-fasel.de" { type forward; forwarders {

        Please let me know if that solution doesn't work for you. In that case, please provide a full set of files as I have done.

        --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}