Here's a perl script that you could run with one or more file names as command line args, and each file would be edited:
#!/usr/bin/perl
use strict;
for my $file ( @ARGV )
{
{
local $/;
open( my $fh, "<", $file ) or do { warn "unable to read $file\
+n"; next };
$_ = <$fh>;
}
s/( \{ \s*
header\("Window-Target:\s*_top"\); \s*
header\("Location:\s*index\.php"\); ) \s*
\}
/$1\n exit;\n}/gx;
{
open( my $fh, ">", $file ) or do { warn "unable to edit $file\
+n"; next };
print $fh $_;
}
}
Note the following:
- using the "x" modifier (so the regex can be arranged more legibly)
- using capturing parens in regex to simplify replacement
- using "\s*" to match all the (potentially variable) white space (
this is the only way you must use something like this to match white space when using the "x" modifier, but you should be handling white space this way in any case)
- using lexically-scoped file handles in minimal blocks
- using local $/ for slurp mode (in same scope as file handle)
- skipping files that can't be read or written (and showing a warning each time)
- if the script is run a second time on the same file, it'll just rewrite the file without changing it (because the regex won't match)
And if I'm taking the trouble to store this edit operation as a little perl script all by itself (which I think is a Good Idea), I would also take the time to add some POD commentary to say why I wrote this script in the first place.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.