469,277 Members | 2,372 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,277 developers. It's quick & easy.

After deleting lines from a file i need to keep the file order

Hello All,

i have a Perl Script that deleting Zones from named.conf file and here is the script
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w 
  2.  
  3. use strict;
  4.  
  5. print "please enter the domain name: ";
  6.  
  7. chomp (my $targetdomain = <STDIN>);
  8.  
  9. my $file = "/home/adam/Desktop/hello"; # copied to my home folder
  10.  
  11. rename $file, "$file.bak" or die "Can't rename file '$file': $!\n";
  12.  
  13. open my $in, '<', "$file.bak" or die "Can't read file '$file': $!\n";
  14. open my $out, '>', $file or die "Can't write file '$file': $!\n";
  15.  
  16. my $comment = 0;
  17. my $block   = 0;
  18.  
  19. while(<$in>) {
  20.     if (/^zone\s+"$targetdomain"/) {
  21.         $comment++;
  22.         $block += () = /(\{)/g;    
  23.     next;
  24.     }
  25.     if($comment) {
  26.         $block += () = /(\{)/g;
  27.         $block -= () = /(\})/g;
  28.         unless ($block) {
  29.             $comment = 0;
  30.             next;
  31.         }
  32.     }
  33.  
  34.       print $out $_ unless $comment;
  35.  
  36. }
the Script simply taking the name of the zone as an input then it deletes the whole zone like that

>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 .
Nov 13 '07 #1
4 1401
KevinADC
4,059 Expert 2GB
See how this works. Uses perls inplace editor to edit the file. Will save a backup copy too.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w 
  2. use strict;
  3. print "please enter the domain name: ";
  4. chomp (my $targetdomain = <STDIN>);
  5. my $file = "/home/adam/Desktop/hello"; # copied to my home folder
  6.  
  7. {
  8.     local @ARGV = ($file);
  9.     local $^I = '.bak';
  10.     MAINLOOP: while(<>) {
  11.         if (/^zone\s+"$targetdomain"/) {
  12.             while(<>) {
  13.                next MAINLOOP if (/^\s*$/);
  14.             }
  15.         }
  16.         else {
  17.             print;
  18.         }
  19.     }
  20. }
Nov 13 '07 #2
Many Thanks it works fine but kindly can you show me how that works ?
i mean can u illustrate the code meaning for me ?

i donot know what is the MAINLOOP and also teh $^I and i will be thankful
Nov 13 '07 #3
KevinADC
4,059 Expert 2GB
Many Thanks it works fine but kindly can you show me how that works ?
i mean can u illustrate the code meaning for me ?

i donot know what is the MAINLOOP and also teh $^I and i will be thankful

You can find $^I and many other perl variables on this page:

http://perldoc.perl.org/perlvar.html

MAINLOOP is a "label". It just tells perl where to go in the code otherwise the inner most enclosing loop is always assumed. Example:

Expand|Select|Wrap|Line Numbers
  1. OUTTER: for (1..10) {
  2.     print $_,"\n";
  3.     INNER: for (20..30) {
  4.       print $_,"\n";
  5.       next OUTTER if ($_ == 25);
  6.    }
Everytime the "INNER" loop gets to 25 it jumps back to the "OUTTER" loop. If you did not use labels, the inner loop would just repeat and continue printing up to 30.
Nov 14 '07 #4
so thanks for your wonderful answers so if the file was messy or something i mean if before the script running the file looks like this

zone "blah.com" {
xxxx
xxxx
xxxx
}


zone "bar.com" {
xxxx
xxxx
xxxxx
xxxxx
}

notice that the file has already 2 blank lines . so how i can make the script to edit to make the file always has only one space between each zone

i have used local $/ = "\n"; as default file separator but it didnot work and thanks again
Nov 14 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by s99999999s2003 | last post: by
14 posts views Thread by micklee74 | last post: by
24 posts views Thread by rudranee | last post: by
8 posts views Thread by Horacius ReX | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.