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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackice
    New Member
    • Oct 2007
    • 13

    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
    [CODE=perl]#!/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+"$targe tdomain"/) {
    $comment++;
    $block += () = /(\{)/g;
    next;
    }
    if($comment) {
    $block += () = /(\{)/g;
    $block -= () = /(\})/g;
    unless ($block) {
    $comment = 0;
    next;
    }
    }

    print $out $_ unless $comment;

    }[/CODE]

    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 .
    Last edited by eWish; Nov 13 '07, 03:27 AM. Reason: Added Language To Code Tag
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    See how this works. Uses perls inplace editor to edit the file. Will save a backup copy too.

    [CODE=perl]#!/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

    {
    local @ARGV = ($file);
    local $^I = '.bak';
    MAINLOOP: while(<>) {
    if (/^zone\s+"$targe tdomain"/) {
    while(<>) {
    next MAINLOOP if (/^\s*$/);
    }
    }
    else {
    print;
    }
    }
    }[/CODE]

    Comment

    • blackice
      New Member
      • Oct 2007
      • 13

      #3
      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

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Originally posted by blackice
        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:



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

        Code:
        OUTTER: for (1..10) {
            print $_,"\n";
            INNER: for (20..30) {
              print $_,"\n";
              next OUTTER if ($_ == 25);
           }
        }
        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.

        Comment

        • blackice
          New Member
          • Oct 2007
          • 13

          #5
          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

          Comment

          Working...