grep + sed for find & replace fun!

2

1

I have a dev copy of a website set up that has quite a few hardcoded references to its live counterpart.

I would like to replace all occurrences of "www." with "dev." in all files.

I think I can use a combination of grep + sed, but I'm not sure how.

user30063

Posted 2010-03-08T15:33:38.430

Reputation:

3Lol it's not programing question. It's basic power user skill. – Maciek Sawicki – 2010-03-08T15:52:03.550

@Maciek: I can buy that! – Josh K – 2010-03-08T17:56:32.753

Answers

2

first try to used sed on one file its sed 's/foo/bar/g' for replacing foo with bar. Then instead of grep i suggest find. Use it to find all files that need changes (*.php, *.html, etc...). Then find have -exec option.

Please make backup of Your project before You start.

Maciek Sawicki

Posted 2010-03-08T15:33:38.430

Reputation: 1 072

2Use sed 's/foo/bar/g' for a global replace. – eleven81 – 2010-03-08T16:08:54.993

Thanks. You are 100% right. with out it in only replace only first instance in a line. I edited my answerer. – Maciek Sawicki – 2010-03-08T16:25:30.647

I'm trying something like this with no success so far: find . -name '*.html' -exec sed 's/www/dev/g' {} ; – None – 2010-03-09T15:12:43.373

FYI I ended up using:

find . -name "*.php" -print | xargs sed -i 's/www./dev./g' – None – 2010-03-09T18:55:20.513