Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: trouble with regular expressions

by Lotus1 (Chaplain)
on Nov 06, 2012 at 16:56 UTC ( #1002524=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re^3: trouble with regular expressions
Re^4: trouble with regular expressions
by programmer.perl (Beadle) on Nov 06, 2012 at 17:51 UTC
    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.

        Yes, I made as you wrote: I closed the file and re-open with > property and all are working)) this is our last code

        #!/usr/bin/perl -w use strict; use warnings; use Cwd qw(); my $path = Cwd::cwd(); my $date = localtime(); my $count=0; my $count_file = 0; my $report = "$path/report_$date.txt"; open (FINDIT, "find $path -name '*.js' -type f -print |") || die "Coul +dn't execute find!\n"; while (my $filename = <FINDIT>) { $count_file++; open (TABLETKA, "$filename") || die "Can't open $filename: $!\n"; my @lines = <TABLETKA>; seek(TABLETKA,0,0); my @clean = <TABLETKA>; foreach (@lines) { if ($_ =~ m#(document\.write\('<iframe.*)(</iframe>'\);)#) + { foreach (@clean) { $count++; if ( s{([[:space:]];document\.write\('<iframe.*)(</iframe> +'\);)}{ //eof} ) { close (TABLETKA); open (APP, ">$filename") || die "Can't open FOR WRITING $f +ilename: $!\n"; print APP @clean; close (APP); open (REPORT, ">>$report") || die "Can't open a report.txt +: $!\n"; chomp($filename); print REPORT "$count_file. FILE: $filename\nINFECTED LINE +NUMBER: $count\nCLEANING DATE: $date\n\n"; close (REPORT); $count = 0; } elsif ( s{([^[:space:]])(;document\.write\('<iframe.*)(</i +frame>'\);)}{$1; //eof} ) { close (TABLETKA); open (APP, ">$filename") || die "Can't open FOR WRITING $f +ilename: $!\n"; print APP @clean; close (APP); open (REPORT, ">>$report") || die "Can't open a report.txt +: $!\n"; chomp($filename); print REPORT "$count_file. FILE: $filename\nINFECTED LINE +NUMBER: $count\nCLEANING DATE: $date\n\n"; close (REPORT); $count = 0; } elsif ( s{(;document\.write\('<iframe.*)(</iframe>'\);)}{ +//eof} ) { close (TABLETKA); open (APP, ">$filename") || die "Can't open FOR WRITING $f +ilename: $!\n"; print APP @clean; close (APP); open (REPORT, ">>$report") || die "Can't open a report.txt +: $!\n"; chomp($filename); print REPORT "$count_file. FILE: $filename\nINFECTED LINE +NUMBER: $count\nCLEANING DATE: $date\n\n"; close (REPORT); $count = 0; } } } } }
        Enough codes make shapes. (Hamidjon)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1002524]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (18)
As of 2014-10-30 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls