Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring slashes in sed find and replace command

Tags:

bash

sed

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

like image 513
majordomo Avatar asked Nov 01 '25 13:11

majordomo


1 Answers

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.

like image 161
Anirudh Ramanathan Avatar answered Nov 03 '25 05:11

Anirudh Ramanathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!