473,381 Members | 1,504 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,381 software developers and data experts.

NEED HELP IN Search comment lines Perl script

Hello all

this thread ihttp://www.thescripts.com/forum/thread50290.html 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 1745
KevinADC
4,059 Expert 2GB
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
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 Expert 2GB
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
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 Expert 2GB
Do you still need help with this question?
Oct 4 '07 #6
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 Expert 2GB
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
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.com" {
#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 Expert 2GB
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
thanks soOooOOOOOooooooOOOOooooOOO much
Oct 6 '07 #11

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

Similar topics

2
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...
1
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...
3
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...
7
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...
20
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...
6
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...
4
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...
4
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...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.