473,748 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NEED HELP IN Search comment lines Perl script

13 New Member
Hello all

this thread ihttp://www.thescripts. com/forum/thread50290.htm l contains what i need but i cannot understand some of the code i need more explain ....


Expand|Select|Wrap|Line Numbers
  1. my $sampleinput = <<"EOF";
  2.  
  3. zone "dontchangethisdomain.com" {
  4. type master;
  5. file "/somepath/to/dontchangethisdomain.com";
  6. notify yes;
  7. }
  8.  
  9. zone "mydomain.com" {
  10. type master;
  11. file "/path/to/zone/mydomain.com";
  12. notify yes;
  13. }
  14.  
  15. zone "leavethisalonetoo.com" {
  16. type master;
  17. file "/other/path/to/leavethisalonetoo.com";
  18. notify yes;
  19. }
  20.  
  21. EOF 
what the need of this however i think itis better to make a file handler to open the file .... !!!!!!


Expand|Select|Wrap|Line Numbers
  1.  my $prematch;
  2. my $thematch;
  3. my $postmatch;
  4.  
why i need that variables pre and the and post i think itis better to choose only the matched string and then just replace it !!!!


any help will be appreciated .....


thanks for all
Oct 4 '07 #1
10 1772
KevinADC
4,059 Recognized Expert Specialist
You have not posted a question or told us what you are trying to do. Forget that stuff you posted and start over. Describe your problem and post the code you are working with.
Oct 4 '07 #2
blackice
13 New Member
sorry for that ....,


I want to write a perl script which will comment out a domain in my named.conf file for Bind.

as a test i made a file in my home directory to perform tests on it ...

the file is /home/blackice/hello

and i wrote this 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. my  $targetdomain = <STDIN>;
  8.  
  9. chomp $targetdomain;
  10.  
  11. my  $file = "/home/blackice/hello";
  12.  
  13. open HAN,$file || die  "error opening file: $!";
  14.  
  15. while (<HAN>) {
  16. if ( /\n^zone "$targetdomain"
  17. \{\n(.*)type(.*);\n(.*)file(.*);\n(.*)notify(.*);\ n(.?)\}/is) {
  18.  
  19. print "hello";
  20.  
  21. }
  22. else
  23. {print "not hello";
  24. }

so i try to make a simple test before making commenting ....

first it take the domain name as an input and if it finds that pattern including the domain name in this file it will print hello if not it will just print not hello

and the hello file contains

Expand|Select|Wrap|Line Numbers
  1.  zone "mydomain.com" {
  2. type master;
  3. file "/path/to/zone/mydomain.com";
  4. notify yes;
  5. }
so when i run the script i got the following

Expand|Select|Wrap|Line Numbers
  1. # perl pth.pl
  2.  please enter the domain name: mydomain.com
  3. not hellonot hellonot hellonot hellonot hellonot hello 

any help will be appreciated ...


and if any one have an idea for commenting that matched strings it will a nice tip for me

thanks
Oct 4 '07 #3
KevinADC
4,059 Recognized Expert Specialist
try this for your test:


Expand|Select|Wrap|Line Numbers
  1. while (<HAN>) {
  2. if  ( /^zone "\Q$targetdomain\E"/ ) {
  3.    print "$_  ( hello )";
  4. }
  5. else {
  6.    print "$_ (not hello)";
  7. }
Oct 4 '07 #4
blackice
13 New Member
thanks for your support

but what about if i want to after searching comments the empty line before the zone name and comment five lines after commenting the empty line before the zone
Oct 4 '07 #5
KevinADC
4,059 Recognized Expert Specialist
Do you still need help with this question?
Oct 4 '07 #6
blackice
13 New Member
aha i need solution
i think iam now very close to the right solution ..

here is the code :-

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w 
  2.  
  3. use strict;
  4.  
  5. print " please enter the domain name: ";
  6.  
  7. my  $targetdomain = <STDIN>;
  8.  
  9. chomp $targetdomain;
  10.  
  11. my  $file = "/home/blackice/hello";
  12.  
  13. open HAN,$file || die "error opening file: $!";
  14. my @fileContent = <HAN>;
  15. close(HAN);
  16.  
  17. for( my $i = 0; $i < @fileContent; $i++ ){
  18.    if( $fileContent[$i] =~ m/^zone\s+"$targetdomain"/ ){
  19.        $fileContent[$i] =~  s/\n/\n#/g;
  20.    }
  21. }
  22.  
  23. open HAN, ">$file";
  24. print HAN @fileContent;
  25. close(HAN);

after run the script th script only comment the second line #type master; only !

and i want to comment all the lines and thanks for you help
Oct 5 '07 #7
KevinADC
4,059 Recognized Expert Specialist
This is very similar to what you are doing. The logic for editing the array can be applied to your code. I used the (core module) Tie::File to edit the file though.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w 
  2.  
  3. use strict;
  4. use Tie::File;
  5. use Fcntl ':flock';
  6.  
  7. print " please enter the domain name: ";
  8.  
  9. my  $targetdomain = <STDIN> ;
  10. chomp $targetdomain;
  11.  
  12. my  $file = "/home/blackice/hello";
  13. my $obj = tie my @array, "Tie::File", $file or die "$!";
  14. $obj->flock;
  15.  
  16. foreach my $i ( 0 .. $#array ){
  17.    if( $array[$i] =~ m/^zone\s+"$targetdomain"/ ){
  18.       $array[$i-1] = '#' unless $i == 0; #first line of file condition
  19.       for my $n (0 .. 5) {
  20.           $array[$i+$n] =  '#' . $array[$i+$n];
  21.       }
  22.    }
  23. }
  24. $obj = undef;
  25. untie @array;
You can see in the "foreach" loop the rather simple logic of moving back or foward "lines" in an array. That can applied directly to your code.
Oct 5 '07 #8
blackice
13 New Member
thanks it works and better than i created
but could you kindly explain me the mechanism of this code and what is Fcntl ':flock'; and how it works i want more explain cause this types of code will help me alot in editing various type of linux files .

and thanks again too much ....
------------------------------------------------------------------------------------------------

back to my code i did some refinements to it

Expand|Select|Wrap|Line Numbers
  1. for( my $i = 0; $i < @fileContent; $i++ ){
  2.     if( $fileContent[$i] =~ m/^zone\s+"$targetdomain"/){
  3.     $fileContent[$i] =~ s/^/#/g;
  4.     $fileContent[$i] =~ s/\n/\n#/g;
$fileContent[$i] =~ s/^/#/g;
$fileContent[$i] =~ s/\n/\n#/g;
this makes me to comment the first line and the second line of the zone just like that ...


#zone "mydomain.c om" {
#type master;

and it didnot comment the other lines is there is any way to add more lines like i added to make all the lines commented as you code does ..

and thanks so much
Oct 5 '07 #9
KevinADC
4,059 Recognized Expert Specialist
As I said at the end of my post:

You can see in the "foreach" loop the rather simple logic of moving back or foward "lines" in an array. That can applied directly to your code.
When you see a module name in code you do not recognize go to CPAN and search for it: CPAN

Type Fcntl in the search box. Then read the modules documentation.
Oct 5 '07 #10

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

Similar topics

2
7460
by: Paul Porcelli | last post by:
Hi folks, I have a perl one-liner embedded in a ksh script. perl -pi.bak -e "s/val/otherval/" inputfile I'd like to check the return code to know if the substitution was successful. If I type: # perl -pi.bak -e "s/val/otherval/" inputfile
1
3115
by: Spamtrap | last post by:
I only do occasional Perl programming and most things I write are short processes. I have something I'm working on that is scanning a text file with about 15 million lines and trying to extract matches from another text file, which has about 170 entries. The second text file is read into an array. The process then scans through the big file for certain possible patterns - it will find those in about 1 out of 25 lines,, when it finds one,...
3
15281
by: FLOTServer | last post by:
Here's my problem: I run a gameserver that runs the game "Medal of Honor". On the game server is log file which contains all of the data from the players for that day (kills, deaths, etc...). I have a perl script that runs on my webserver, which is supposed to login to the gameserver and download the log. The problem is that when it gets to the part where it needs to download the file, it aborts. The gameserver FTP requires I use port 24...
7
4389
by: Mike Kamermans | last post by:
I hope someone can help me, because what I'm going through at the moment trying to edit XML documents is enough to make me want to never edit XML again. I'm looking for an XML editor that has a few features that you'd expect in any editor, except nearly none of them seem to have: 1 - Search and repalce with Regular Expressions. 2 - Search and Replace in an Xpath context. 3 - User specified tag-generation for either on a...
20
4282
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
6
2316
by: blackice | last post by:
hello all ..., first i would thank all the people who are in this forums cause they are very helpful ..., By the way i had some posts hers about comment out the named.conf file the script is simply comment out the zones in the file and it works well while i was testing scrpt i found that the script is working in an fixed way which means that the zone must be 5 lines if it not a five line the script won't function as needed ., Here...
4
1537
by: blackice | last post by:
Hello All, i have a Perl Script that deleting Zones from named.conf file and here is the script #!/usr/bin/perl -w use strict; print "please enter the domain name: "; chomp (my $targetdomain = <STDIN>);
4
1914
by: mpatharkar | last post by:
Hi all, I wrote a script to search a pattern in input file and if pattern does not found in input file ,print that pattern in to output file. The input file is -------------------------------------------------------- 1999-1011»All the flowers of tomorrow are in the seeds of today. 2000-1209»I saw this article posted on another site and wanted to share it with everyone at TSDN. Please read this article and post your thoughts....
1
2933
by: vikjohn | last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified CGI application misbehaved by not returning a complete set of HTTP headers. The scripts are very long but here are the opening statements: The One that works .... #!C:\Perl\bin\perl.exe # openresolver.cgi # # OpenResolver - a CGI script for...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9386
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
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,...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
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();...
1
3319
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
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
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.