Search Engine Optimization > Webmaster World > For the FAQ? (gsr.pl re-post)
For the FAQ? (gsr.pl re-post)
Posted by Matt Probert on June 9th, 2004

Heidi;

You might have missed this, as it was buried in a thread which had
become a little silly!

The following Perl script works across all platforms which have Perl
installed, and provides a free search and replace facility which
people keep on asking for, thought you might like to put it in the
FAQ?


#!/usr/bin/perl

# A quick and dirty global search and replace program written in Perl
# Supplied as is without warranty
# Usage is 'perl gsr.pl <file spec> <find> <replace>

# There is plenty of scope for improvements, not least of which
# is modifying the program to span line breaks!

# Note:
# A bug in Windows means that file names in lower case, which
# conform to the 8.3 naming convention will be converted to
# uppercase by this program. eg: myfile.htm will be renamed
# MYFILE.HTM

die "Usage is $0 <file spec> <search> <replace>\n" if $#ARGV != 2;

# Extract file names matching ARGV[0] into a list
@fnames=glob($ARGV[0]);

foreach $file (@fnames)
{
open(FILE, $file);
open(TEMP,">temp");
while (<FILE>)
{
# Replace all occurences of the second command line
argument
# with the second within the current line in the file

$_ =~ s/$ARGV[1]/$ARGV[2]/gi;

print TEMP "$_";
}
close(FILE);
close(TEMP);
rename(temp,$file);
}

Posted by Kenneth on June 9th, 2004

On 2004-06-09, Matt Probert <comments@probertencyclopaedia.com> wrote:
Why reinvent the wheel.

replace "this phrase" "that phrase" -- <filespec>

ken

Posted by Matt Probert on June 9th, 2004

On Wed, 09 Jun 2004 06:25:40 GMT Kenneth <Kenneth@pickone.anyone>
broke off from drinking a cup of tea at None to write:

Because if that's a wheel, it wont roll!

The Windows supplied 'replace' command copies files, it doesn't do a
search and replace within them!

Matt


Posted by Kenneth on June 9th, 2004

On 2004-06-09, Matt Probert <comments@probertencyclopaedia.com> wrote:
I wasn't referring to the windows replace command!

I was referencing the replace command that comes with mysql, windows
and linux version. If you have perl installed on a windows box...chances are
you have mysql installed.

Or, there is also a simple GUI replace for windows that closely mimics the
mysql replace program.


ken






Posted by Matt Probert on June 9th, 2004

On Wed, 09 Jun 2004 15:11:22 GMT Kenneth <Kenneth@pickone.anyone>
broke off from drinking a cup of tea at None to write:

Oh I see. Not using SQL, let alone 'mysql' I had not encountered it,
and guessing from the vast number of requests for "is there a program
to change this text to that" that we get here, I guess a substantial
number of other people haven't either.

I'm sure. There are lots of applications that do it. But that doesn't
stop people asking here for them continuously. Hence, bung a free one
in the FAQ, and that should answer that frequently asked question!

Matt


Posted by Kenneth on June 9th, 2004

On 2004-06-09, Matt Probert <comments@probertencyclopaedia.com> wrote:
People don't read the docs.

Well, now the world knows it's there, it's free and it works.

ken


Funbolt.com - Entertainment portal, wallpapers, sexy celebs