Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i am looking for a way to replace a string in a file with a string that contains a slash by using sed.

connect="192.168.100.61/foo"
srcText="foo.bar=XPLACEHOLDERX"
echo $srcText | sed "s/XPLACEHOLDERX/$connect"

the result is

sed: -e Expression #1, Character 32: Unknown option for `s'
share|improve this question
up vote 27 down vote accepted

Use another character as delimiter in the s command:

echo $srcText | sed "s|XPLACEHOLDERX|$connect|"

Or escape the slashes with parameter expansion:

echo $srcText | sed "s/XPLACEHOLDERX/${connect//\//\/}/"
share|improve this answer
    
Awsome solution! Didn't know sed is that flexible =) – martin Jun 1 '12 at 12:34
    
+1 for second way. first one does not work on freebsd. – ibrahim Jul 8 '14 at 11:11

If your shell supports it:

"${srcText/XPLACEHOLDERX/$connect}"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.