firewall00 has asked for the wisdom of the Perl Monks concerning the following question:

hello all


first i would thank this great forum which helped me many times
by the way i had another thread before about commenting zone in the named.conf file ,, and now i want to delete lines instead of commenting only
first i have tried many times ,, i have stored the file in array and then try to loop throw this array to delete specific lines but this failed .
so i reuse the code i have used to make the commenting
#!/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 $comment =0; my $block = 0; while(<$in>) { if (/^zone\s+"$targetdomain"/) { s// /; $comment++; $block += () = /(\{)/g; } if($comment) { $block += () = /(\{)/g; $block -= () = /(\})/g; $comment = 0 unless $block; } print $out $_; }
i want to delete all the lines between the two open brackets so i provide the block to count the open brackets and also closed brackets
could any one tell me how can i think about that and i will be thankful

Replies are listed 'Best First'.
Re: Deleting lines from named.conf file
by toolic (Bishop) on Nov 10, 2007 at 02:29 UTC
    It is good that you have shown some code. However, I am still not certain what you are trying to accomplish.

    If you are trying to delete lines within left and right curly braces, {}, then you could use Range Operators:

    #!/usr/bin/env perl use warnings; use strict; while (<DATA>) { unless (/{/ .. /}/) {print} } __DATA__ keep { junk junk2 junk3 } keep keep2 keep3 { junk4 } keep4
    This will delete all the text within the curly braces (the "junk" text), and will preserve all the text outside the braces (the "keep" text). The output of this script is:
    keep keep1 keep2 keep3 keep4
    If this does not help, perhaps you could provide a small sample of your input "hello" file, and show which line you want to delete from that file.
      thanks for your help but exactly what i want to make is to delete a certain zone from a file the zone will be provided
      by the user input which i assigned at the start of my code
      and then uses the $block variable to count the range of the curly brackets until the end of the zone
      Example for what iam taking about
      zone "foo.com" { type master; file path to file ; }; zone "bar.com" { type master; file path to file ; additional informations { blah blah blab } } zone "perl.com" { type master; file path to file; }
      i want to delete lines from start of the the zone which is provided by targetdomain variable which is entered by the user
      after the script is running assume that the targetdomain was = bar.com the output
      zone "foo.com" { type master; file path to file ; }; zone "perl.com" { type master; file path to file; };
      and thanks for your care
Re: Deleting lines from named.conf file
by eric256 (Parson) on Nov 09, 2007 at 23:31 UTC

    Your print isn't conditional so it will always print out. I think you want to change it to something like print $out $_ unless $block;


    ___________
    Eric Hodges
      thanks for your replay but this didnot work :(
      i think i need to provide something to link between the block and the statement that makes the deletion as in the example of the commeting
        here is what exactly i want to make and hope you will show me how i can do that real
        #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
        sorry for this long thread but i write what i think just making a brain storming to find a way with you
        thanks
Re: Deleting lines from named.conf file
by shmem (Chancellor) on Nov 10, 2007 at 14:27 UTC
    Maybe zonemaster is for you.

    --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}
      hello Shmem how are i would thank your for you help
      but i cannot really understand the code of the zone master the link you provided in your replay
      and as i saw this zone master depend on connection to database not reladted to a file .
      and i will be thankful if you could help me
        The answers to your very first post here provided all you need to do the task. I gave you a solution which can be tweaked to not comment lines out, but not print them instead (untested):
        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; }

        Since that trivial task seems to be too difficult for you, I gave you a link to a tool which handles all things zone file related. Yes, it relies on a database, but if you have to do more than casual changes to zone files, for which you could also fire up your editor and do changes by hand, it is the right way to go.

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