By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,426 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

Deleting the matchin pattern form all the files

100+
P: 123
Hi..
I have one directory which contains huge amount of .php files.
I want to delete
Expand|Select|Wrap|Line Numbers
  1. <p align="right"><input name="closeme" type="button" value="Close Window" onclick="self.close()" /></p> <br />
---- Close window code form each of the pages.
So i need some perl subroutine which can do that for all the files in the directory..
Apr 16 '08 #1
Share this Question
Share on Google+
5 Replies


eWish
Expert 100+
P: 971
You will need to so us what you have tired. We will assist those who try and get stuck.

--Kevin
Apr 16 '08 #2

100+
P: 123
Hi ..I tried out tht 1..Bt shows me errors..There may be some errors as i just started working on perl.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perL
  2. <!-- use File::Copy qw(copy);-->
  3.  
  4. use strict;
  5.  
  6. my $re_file = 'pattern.php';
  7. my $dir_name = "home/davea/test";
  8.  
  9. opendir(DIR, $dir_name) or die "Can't open $dir_name: $!";
  10. while (readdir(DIR)) {
  11.         my $file = $_;
  12.         print REPORTFH "$file\n";
  13. open(REPORTFH, "> $re_file") or die "Can't read $re_file: $!";
  14.  
  15. while (readdir(DIR)) {
  16.         my $file = $_;
  17.         print REPORTFH "$file\n";
  18.  
  19.         if (! -e "$dir_name/$file") {
  20.                 print "Can't find the file $file. Please verify in the $re_file\n";
  21.         } elsif ($file =~ /PC/) {
  22.                 my $source = "$dir_name/$file.php";
  23.                 my $target = "$dir_name/$file";
  24.  
  25.                 copy($target, $source) or die "not working";
  26.                 open(IN, $source) or die "Can't open $source: $!";
  27.                 open(OUT, "> $target") or die "Can't open $target: $!";
  28.  
  29.                 my $truncateMode = 0;
  30.  
  31.                 while (IN) {
  32.                         if (! $truncateMode) {
  33.                                 if (/ <p align="right"><input name="closeme" type="button" value="Close Window" onclick="
  34. self.close() /)
  35.                                  {
  36.                                         $truncateMode = 1;
  37.                                 } else {
  38.                                         print OUT $_;
  39.                                        }
  40.                                             }
  41.                            }
  42.                 }
  43.  
  44.                 close (IN);
  45.                 close (OUT);
Apr 16 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Are you trying to embed that into an html file? You can't. This will also cause perl to drop dead:

<!-- use File::Copy qw(copy);-->

as it is a syntax error and will not allow the perl script to compile.
Apr 16 '08 #4

100+
P: 123
Are you trying to embed that into an html file? You can't. This will also cause perl to drop dead:

<!-- use File::Copy qw(copy);-->

as it is a syntax error and will not allow the perl script to compile.
No , I have created .pl file and not the HTML and that is using VI editor...and the target files are PHP pages from which i have to remove the desire code

and when i run the code it gives me below errors :

Bareword "IN" not allowed while "strict subs" in use at close1.pl line 42.
Global symbol "$source" requires explicit package name at close1.pl line 47.
Global symbol "$source" requires explicit package name at close1.pl line 47.
Unmatched right curly bracket at close1.pl line 49, at end of line
syntax error at close1.pl line 49, near "}"
Execution of close1.pl aborted due to compilation errors.
Apr 16 '08 #5

eWish
Expert 100+
P: 971
Maybe this will help. I have commented sections of the code to help understand what is taking place.

Expand|Select|Wrap|Line Numbers
  1. # Directory to read
  2. my $my_directory = '/path/to/dir';
  3.  
  4. # Pattern for the search and replace
  5. my $pattern = qr/pattern here/;
  6.  
  7. # Get the files in said directory
  8. opendir(my $DIR, $my_directory) || die "Can't open $my_directory: $!\n";
  9. my @list = readdir($DIR);
  10. closedir($DIR);
  11.  
  12. # Loop through the files
  13. foreach  my $file (@list) {
  14.  
  15.         # Exclude '.' and '..'
  16.         next if $file eq '.'  || 
  17.                 $file eq '..';
  18.  
  19.     # Split the filename into the name and extension                        
  20.     my ($name, $ext) = split(/\./, $file);
  21.  
  22.     # Open new and existing file    
  23.     open(my $NEW_FILE, '>', "$my_directory/$name" . '_temp.' . $ext) || die "Can't open file", $name . '_temp.' . $ext, ':', $!, "\n";
  24.     open(my $OLD_FILE, '<', "$my_directory/$file") || die "Can't open file $file: $!\n";
  25.     while (<$OLD_FILE>) {
  26.  
  27.         # Replace the data as desired
  28.         s/$pattern//mgx;
  29.  
  30.         # After replacement the print to the file
  31.         print $NEW_FILE $_;
  32.     }
  33.  
  34.     # Close the files
  35.     close($OLD_FILE);
  36.     close($NEW_FILE);
  37.  
  38.     # Delete the original file
  39.     unlink("$my_directory/$file") || die "Can't unlink/delete $file: $!";
  40.  
  41.     # Rename the new file to the same as the original file
  42.     rename("$my_directory/$name" . '_temp.' . $ext, "$my_directory/$file")  || die "Can't rename $file: $!";
  43.  
  44. }
--Kevin
Apr 17 '08 #6

Post your reply

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