Created
August 28, 2009 16:44
-
-
Save textarcana/177086 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
=head1 NAME | |
Search and Replace | |
=head1 SYNOPSIS | |
perl search-replace.pl <files> | |
=head1 DESCRIPTION | |
Search and replace against a hard-coded regex. We'll use this script | |
to replace JavaScript image paths. Maybe later it can | |
be adapted into something more flexible. | |
=head2 Caveats | |
Silent in-place replacement without backup. Back up your assets (in | |
source control) before testing this script. | |
=head2 The original one-liner | |
perl -pi.bak -e 's/("http:\/\/images.example.com)([^:?<>]+\.(gif|jp(e)?g|png))/"\047 + \$.example.foo + "$2" + \047/g' global.js | |
=cut | |
$^I=".bak"; # change the files themselves instead of writing to STDOUT | |
# use 'bak' as the extension for backup files | |
while (<>) | |
{ | |
s{ | |
"http://images.example.com # match a double quote, followed by | |
# the hard-coded URL we want to replace | |
( # now start capturing to the $1 backreference variable | |
[^:?<>]+ # match any character except those not legal in file paths | |
\. # followed by a literal dot, followed by | |
(gif|jp(e)?g|png) # any of the usual image file extensions | |
) # stop capturing | |
}{"' + \$.example.foo + '$1}xg; # print back a properly quoted JavaScript string of the form: | |
# ‘ + $.example.foo + “<pathname>” + ’ | |
print $_; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment