469,276 Members | 1,687 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,276 developers. It's quick & easy.

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

Post your reply

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

Similar topics

2 posts views Thread by Paul Porcelli | last post: by
1 post views Thread by Spamtrap | last post: by
3 posts views Thread by FLOTServer | last post: by
1 post views Thread by vikjohn | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.