Take the 2-minute tour ×
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.

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
add comment

2 Answers

up vote 20 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
add comment

If your shell supports it:

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

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.