http://www.perlmonks.org?node_id=1002521


in reply to Re: trouble with regular expressions
in thread trouble with regular expressions

Hi 2teez,

my last solution was (anyway it puts a js comment "//" and next codes are invalid, but here were an unwanted bits in HD memory)

#!/usr/bin/perl -w use 5.010; use strict; use warnings; open (FINDIT, "find /home/auu/Documents/js -name '*.js' -type f -print + |") || die "Couldn't execute find!\n"; while (my $filename = <FINDIT>) { open (TABLETKA, "+<$filename") || die "Can't open a file $filename: $! +\n"; my @lines = <TABLETKA>; foreach (@lines) { print "$filename is cleaned\n" if s{(\;do.*)(</iframe>'\);)}{\; //eof} +; } seek(TABLETKA,0,0) || die; print TABLETKA @lines; close (TABLETKA); }

this is (above) is my final code, now I'm thinking how to integrate your code to my code... do you have any idea? ))...

Enough codes make shapes. (Hamidjon)

Replies are listed 'Best First'.
Re^3: trouble with regular expressions
by Lotus1 (Vicar) on Nov 06, 2012 at 16:56 UTC

    When you overwrite the original file with less text it doesn't clear out the original text, that is where the extra stuff after //eof comes from. Try writing to a new file to see this for yourself.

      Yes, you are right, I'm happy that we found the bug... Now, but I have to save to file itself, I can't (musn't) write to another file...
      Enough codes make shapes. (Hamidjon)
        You can't do what you are trying to do with the file...unless you write out exactly the same number of characters as were there before. Usually instead you open a new file, write out all the lines (whether changed or unchanged) to it, then rename it to replace the existing file. For a quick script operating on only small files, you could use Tie::File to automate most of this for you.
        --
        A math joke: r = | |csc(θ)|+|sec(θ)|-||csc(θ)|-|sec(θ)|| |

        It isn't a safe thing to do but you could close the file after reading it into your array. Then re-open it for output open $fh, ">", "file.txt" or similar which will clobber (clear the contents) your file then write to it. The danger is that a minor mistake in your program could wipe out or mangle the original file. You could write to a test file until you finish debugging.