4

I'm trying to do a find and replace using sed in a terminal.

Within the apache config file, I'm trying to replace:

DocumentRoot /var/www

with

DocumentRoot /var/www/mysite.com/public_html

From the command line, without using an editor. I'd like to do this with sed, and I've tried various iterations of:

sudo sed -i 's/'DocumentRoot /var/www'/'DocumentRoot /var/www/mysite.com/public_html'/' /etc/apache2/sites-available/mysite.com

However when doing so I get this error: sed: -e expression #1, char 14: unterminated s command

So it's erroring on the slashes in the paths. How do I get around this?

Thanks for your help

majordomo
  • 1,160
  • 1
  • 15
  • 34
  • possible duplicate of [Sed - unknown option to \`s'](http://stackoverflow.com/questions/9366816/sed-unknown-option-to-s) – tripleee Apr 22 '13 at 04:26
  • Note that in your apache configuration file, there is often a directive like: ... some options ... and you need to change the path there as well. – eminor Apr 22 '13 at 04:32

2 Answers2

10

It isn't necessary to use / as the regex delimiter. You can use # or @ when your regular expressions contain / that you would otherwise need to escape.

sed -i s#expr1#expr2#

So, it could be:

sudo sed -i 's#DocumentRoot /var/www#DocumentRoot /var/www/mysite.com/public_html#' /etc/apache2/sites-available/mysite.com

Also, as your substitution-argument itself contains spaces, you need to enclose them in single-quotes.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • minor edit to yours worked: `sudo sed -i 's#DocumentRoot /var/www#DocumentRoot /var/www/mysite.com/public_html#' /etc/apache2/sites-available/mysite.com` – majordomo Apr 22 '13 at 04:26
  • @gcubed Looks like I placed the delims in the wrong places, for your requirement. Glad it worked :) – Anirudh Ramanathan Apr 22 '13 at 04:39
4

It is not compulsory to use / as separator in sed. sed 's/hi/hey/ is same as sed 's#hi#hey#'