开发者

Find and replace a URL with grep/sed/awk?

开发者 https://www.devze.com 2023-04-08 08:35 出处:网络
Fairly regularly, I need to replace a local url with a live in large WordPress databases. I can do it in TextMate, but it often takes 10+ minutes to complete.

Fairly regularly, I need to replace a local url with a live in large WordPress databases. I can do it in TextMate, but it often takes 10+ minutes to complete.

Basically, I have a 10MB+ .sql file and I want to:

Find: http://localhost:8888/mywebsite

and

Replace with: http://mywebsite.com

After that, I'll save the file and do a mysql import to the local/live se开发者_StackOverflow中文版rvers. I do this at least 3-4 times a week and waiting for Textmate has been a pain. Is there an easier/faster way to do this with grep/sed/awk?

Thanks!

Terry


sed 's/http:\/\/localhost:8888\/mywebsite/http:\/\/mywebsite.com/g' FileToReadFrom > FileToWriteTo

This is running switch (s/) globally (/g) and replacing the first URL with the second. Forward slashes are escaped with a backslash.


kent$  echo "foobar||http://localhost:8888/mywebsite||fooooobaaaaaaar"|sed 's#http://localhost:8888/mywebsite#http://mywebsite.com#g'
foobar||http://mywebsite.com||fooooobaaaaaaar

if you want to do the replace in place (change in your original file)

sed -i 's#http://.....#http://mysite#g' input.sql


You don't need to replace the http://

sed "s/localhost:8888/www.my-awesome-page.com/g" input.sql > output.sql

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号