Results 1 to 3 of 3

Thread: Help with escaping characters using sed

  1. #1
    Join Date
    Mar 2006
    Location
    Canada
    Beans
    1,313
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Question Help with escaping characters using sed

    I need to do a search and replace, along with a lot of other patches, on some of my work's software but I can't figure out how/what characters to escape.

    I'm trying to find
    $_SERVER['DOCUMENT_ROOT']
    with
    '/home/brett/www/svn/'
    I've tried running sed with many, all, some characters escaped but it keeps replacing characters and phrases I don't want changed.

    Example:
    Code:
    sed --in-place -e "s/$_SERVER['DOCUMENT_ROOT']/'\/home\/brett\/www\/svn\/'/g" code.php
    Code:
    sed --in-place -e "s/$_SERVER[\'DOCUMENT_ROOT\']/\'\/home\/brett\/www\/svn\/\'/g" code.php

  2. #2
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Help with escaping characters using sed

    Hi, you don't have to use / as the delimiter between the search and replace expressions, for example you could use #, and then you wouldn't need to escape the slashes in your path. You'll need to escape the $ and [ chars in your search expression. Something like this:
    Code:
    sed -e "s#\$_SERVER\['DOCUMENT_ROOT']#'/home/brett/www/svn/'#" somefile

  3. #3
    Join Date
    Mar 2006
    Location
    Canada
    Beans
    1,313
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Help with escaping characters using sed

    Quote Originally Posted by DaithiF View Post
    Hi, you don't have to use / as the delimiter between the search and replace expressions, for example you could use #, and then you wouldn't need to escape the slashes in your path. You'll need to escape the $ and [ chars in your search expression. Something like this:
    Code:
    sed -e "s#\$_SERVER\['DOCUMENT_ROOT']#'/home/brett/www/svn/'#" somefile
    That's fantastic and works! Thank you!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •