473,698 Members | 2,631 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

13 New Member
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 1530
KevinADC
4,059 Recognized Expert Specialist
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
blackice
13 New Member
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 Recognized Expert Specialist
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
blackice
13 New Member
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
4021
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 load the data by something like that: LOAD FROM "/pathto/artikel.data" OF DEL MODIFIED BY DATESISO CHARDEL0xbf SAVECOUNT 50000 MESSAGES "/pathto/artikel.msg" INSERT INTO artikel;
1
1180
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, there is a begin, cancel, begin, and end record, along with a time. I need to keep only the second Begin record (3) and the End record (4); I'm not concerned about records 1 and 2. Some sort of thing that finds a cancel record, and deletes the...
5
2685
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. But when I installed the app from a CD-ROM (Release folder is in D:), when I remove the app in Control Panel these problems occur: Control Panel does not delete the application folders. When I try to delete them I get message "Cannot delete file:...
5
3496
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 case. One handle remains open. Debugging shows that it's actually the IIS cache and not ASP.NET that owns this handle. During IIS shutdown, the handle is closed with the following stack trace: ChildEBP RetAddr 0006fbe4 5a403e05...
5
14604
by: s99999999s2003 | last post by:
hi i have a file something like this abcdefgh ijklmnopq 12345678 rstuvwxyz ...... ......
14
1678
by: micklee74 | last post by:
hi say i have a text file line1 line2 line3 line4 line5 line6 abc
24
6140
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 the code please.
8
2210
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 what do you think is the fastest method to delete specific lines in a text file ? Thanks
0
1198
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. The .BAK files have a "Keep for" time of 5 days, and that seems to be working Fine Business...the .TRN files have a "Keep for" time of 7 days, but they seem to be not deleting after that timeframe...or at all...when I noticed (after spilling over into...
1
8904
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.