Reputation: 157
I'm part of a rotating team that manages a lot of websites, and we inherited some particularly bad code for one website that we're in the process of completely redesigning. Horrifically enough, there are links on the development server that lead you to the live server and to old domains and a lot of other terrible, terrible things.
I've been trying to write a grep/sed command to replace all of these links with the user-defined php function full_link that we use across our websites now to prevent all the linking to different places. So (using a placeholder for our domain) instead of writing http://www.place.com/foo/bar
, you'd write <?php echo full_link('foo/bar'); ?>
and it will work when we move it from one server to another.
Here's what I got:
grep -v 'echo' * -r -P | grep "(?<=<a href=['\"])(http:\/\/foo\.bar\.net\/|10\.41\.6\.118\/|http:\/\/foo2\.bar\.net\/)([^<]*?)(?=['\"])" -P | sed -r "s@(?<=<a href=['\"])(http://foo\.bar\.net/|10\.41\.6\.118/|http://foo2\.bar\.net/)([^<]*?)(?=['\"])@<?php echo full_link('\2'); ?>@gpw output"
(If you're wondering about the first grep or the [^<]
, they're both just a basic attempt to keep from putting php tags inside existing php tags. Since it's just a first pass to make manual editing less full of copy-pasting links and getting redirected to the wrong server, it doesn't need to be perfect, but I am open to better ways to do that.)
I've got the grep statements working and picking up what I want them to get, but when I add the sed to the end, this is what happens:
sed: -e expression #1, char 159: Invalid preceding regular expression
From what research I've done, it seems like I probably escaped something in my sed statement incorrectly, and I've tried a number of things, but it only ever gives me the same message pointing to one of the last few characters of the expression.
Upvotes: 3
Views: 2741
Reputation: 1
sed "s.http://[^/]*/.<?php echo full_link('.;s/$/'); ?>/"
http://[^/]*/
<?php echo full_link('
$
'); ?>
Upvotes: 1