On Thu, 21 Mar 2002, Nick Wilson wrote:

> Hi everyone, 
> could someone please help me solve the following problem?
> 
> I need to recursively find and replace a string in a whole bunch of dirs
> (a website). For example: 
> 
> change: http://my-testing-environment.com
> to:     http://www.the-real-website.com
> 
> I gues sed is the tool for this right? I've read the man page but I'm
> somewhat intimidated by it :-)

Well, you can use sed, but using perl gives one big advantage; it lets you 
edit files in-place.  Actually, it's making a copy first and then 
overwriting the original, but you don't have to deal with that anyway.  
Here is the command you need:

perl -p -i -e 's/testing-environment/the-real-website/g' `find . -type f`

Here's the breakdown:
-p means work like a filter: input text, run whatever program I specify on 
each line of text, then print out each line.  It is the equivalent of 
wrapping your program with "while(<>) {" and "}".

-i means edit files in-place.  You can specify an enxtention to leave a 
backup of the original with if you want

-e means the next parameter is the program

The program is the equivalent to:
while(<>)
{
        $_ =~ s/testing-environment/the-real-website/g;
        print $_;
}

In -p -i mode, the program should be followed with a list of files to 
process.  Here I have the find command list all regular files under the 
current directory in a subshell.

Does this do what you want?

----------------------------------------------------------------------------
DDDD   David Kramer         [EMAIL PROTECTED]       http://thekramers.net
DK KD  
DKK D  I tried so hard and got so far  But in the end it doesn't even matter
DK KD  I had to fall to lose it all   But in the end it doesn't even matter
DDDD                                                            -Linkin Park



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to