Forum Moderators: coopster & phranque

Message Too Old, No Replies

FreeBSD Search and Replace

         

Ept103

1:04 am on Mar 8, 2005 (gmt 0)

10+ Year Member



I'm running a linux web server and want to do a large amount of find and replace's..

Example:

Want to search all files for the string "http://www.example.com" and replace that string with "http://www.widgets.com"

Are there any scripts floating around to do this for me? I'm aware of the regexp commands etc, but I need to be able to just input a string and replacement and push go..

This is going to be used to replace URLs, so it must be capable of dealing with special characters such as: ":/.-'>< and spaces.

kpaul

1:20 am on Mar 8, 2005 (gmt 0)

10+ Year Member



yourprompt> perl -pi.bak -e s#http://.*?/##g `find . -name "*.html"`

the above strips out all the occurences of the [domain.com...] on all the files ending in .html and makes a backup file with the .bak extension...

this should get you started. google perl one liners find replace or something similar.

HTH,
kpaul

Ept103

1:47 am on Mar 8, 2005 (gmt 0)

10+ Year Member



It's making a little bit more sense to me now..

Still think I need a script where I can input regular text without worrying about special characters though, since my strings are going to change all the time.

Any ideas where to find a script that does this? Maybe one that I can upload to my server and change the string and replacement each time and it will do commands and special character replacements for me? Is this even possible?

kpaul

2:03 am on Mar 8, 2005 (gmt 0)

10+ Year Member



yeah. you should be able to pick one up on one of the free script sites out there...

if nothing else, if wouldn't be too hard to write and would be a great learning experience ;)

basically:

1) read directory
2) open files, one by one
3) user regex on each line of each file

there's more than one way to do it with Perl,
-kpaul