Deleting the matchin pattern form all the files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajd335
    New Member
    • Apr 2008
    • 123

    Deleting the matchin pattern form all the files

    Hi..
    I have one directory which contains huge amount of .php files.
    I want to delete
    Code:
    <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..
    Last edited by eWish; Apr 16 '08, 05:53 PM. Reason: Please use code tags
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    You will need to so us what you have tired. We will assist those who try and get stuck.

    --Kevin

    Comment

    • ajd335
      New Member
      • Apr 2008
      • 123

      #3
      Hi ..I tried out tht 1..Bt shows me errors..There may be some errors as i just started working on perl.

      [CODE=perl]#!/usr/bin/perL
      <!-- use File::Copy qw(copy);-->

      use strict;

      my $re_file = 'pattern.php';
      my $dir_name = "home/davea/test";

      opendir(DIR, $dir_name) or die "Can't open $dir_name: $!";
      while (readdir(DIR)) {
      my $file = $_;
      print REPORTFH "$file\n";
      open(REPORTFH, "> $re_file") or die "Can't read $re_file: $!";

      while (readdir(DIR)) {
      my $file = $_;
      print REPORTFH "$file\n";

      if (! -e "$dir_name/$file") {
      print "Can't find the file $file. Please verify in the $re_file\n";
      } elsif ($file =~ /PC/) {
      my $source = "$dir_name/$file.php";
      my $target = "$dir_name/$file";

      copy($target, $source) or die "not working";
      open(IN, $source) or die "Can't open $source: $!";
      open(OUT, "> $target") or die "Can't open $target: $!";

      my $truncateMode = 0;

      while (IN) {
      if (! $truncateMode) {
      if (/ <p align="right">< input name="closeme" type="button" value="Close Window" onclick="
      self.close() /)
      {
      $truncateMode = 1;
      } else {
      print OUT $_;
      }
      }
      }
      }

      close (IN);
      close (OUT);[/CODE]
      Last edited by eWish; Apr 16 '08, 10:16 PM. Reason: Please use code tags

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        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.

        Comment

        • ajd335
          New Member
          • Apr 2008
          • 123

          #5
          Originally posted by KevinADC
          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.

          Comment

          • eWish
            Recognized Expert Contributor
            • Jul 2007
            • 973

            #6
            Maybe this will help. I have commented sections of the code to help understand what is taking place.

            [CODE=perl]# Directory to read
            my $my_directory = '/path/to/dir';

            # Pattern for the search and replace
            my $pattern = qr/pattern here/;

            # Get the files in said directory
            opendir(my $DIR, $my_directory) || die "Can't open $my_directory: $!\n";
            my @list = readdir($DIR);
            closedir($DIR);

            # Loop through the files
            foreach my $file (@list) {

            # Exclude '.' and '..'
            next if $file eq '.' ||
            $file eq '..';

            # Split the filename into the name and extension
            my ($name, $ext) = split(/\./, $file);

            # Open new and existing file
            open(my $NEW_FILE, '>', "$my_direct ory/$name" . '_temp.' . $ext) || die "Can't open file", $name . '_temp.' . $ext, ':', $!, "\n";
            open(my $OLD_FILE, '<', "$my_direct ory/$file") || die "Can't open file $file: $!\n";
            while (<$OLD_FILE>) {

            # Replace the data as desired
            s/$pattern//mgx;

            # After replacement the print to the file
            print $NEW_FILE $_;
            }

            # Close the files
            close($OLD_FILE );
            close($NEW_FILE );

            # Delete the original file
            unlink("$my_dir ectory/$file") || die "Can't unlink/delete $file: $!";

            # Rename the new file to the same as the original file
            rename("$my_dir ectory/$name" . '_temp.' . $ext, "$my_direct ory/$file") || die "Can't rename $file: $!";

            }[/CODE]

            --Kevin

            Comment

            Working...