473,416 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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

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

Similar topics

5
by: Olaf Gschweng | last post by:
We're new into DB2 and have some problem with DB2 8.1 (?) on a Linux system. We load some big tables of a DB2 database from files every day. We do a "DELETE FROM table" for each table and then we...
1
by: Jim | last post by:
Hi all, having a problem with a database. I have a feed from a mainframe that sends over data in the following format, I've numbered the rows 1 through 8 for this message. Under Entry 208,...
5
by: George | last post by:
VB.net 2003 standard, XP windows home edition. Installed first application OK today. When I removed the application via Control Panel, there were no problems and the app folders were deleted. ...
5
by: Sam777 | last post by:
I was under the impression that creating the app_offline.htm file at the root of the webapp would cause all handles to be closed so that the app could be removed. Unfortunately, this isn't the...
5
by: s99999999s2003 | last post by:
hi i have a file something like this abcdefgh ijklmnopq 12345678 rstuvwxyz ...... ......
14
by: micklee74 | last post by:
hi say i have a text file line1 line2 line3 line4 line5 line6 abc
24
by: rudranee | last post by:
hi there, can anyone tell me how to lines from a file which are odd numbered i.e. 1st,3rd,5th...lines. i tried incrementing file pointer by 2 (fp=fp+2) but it does'nt work Can someone give me...
8
by: Horacius ReX | last post by:
Hi, I need to write a program which reads an external text file. Each time it reads, then it needs to delete some lines, for instance from second line to 55th line. The file is really big, so...
0
by: DanInTacoma | last post by:
We have a fairly straightforward maint plan on one of our servers (MSSQL 2K)...it backs up several DB's to a folder on another server (the one with the tape drive) so they can be archived to tape. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.