i am trying to replace content of file using sed by following commands
searchString='(<property *name="sourceUrl" *value=")[^?]*~'
replacementString="file:///tmp/abc-efg"
sed -i 's~${searchString}1${replacementString}~g' $file
but it is giving
sed: -e expression #1, char 42: unterminated `s' command
Advertisement
Answer
You’re missing a separator (which is ~ in your case). It looks like you are trying to put it on the end of $searchString, which is strange. I don’t know why you’re doing that. The reason it doesn’t work is because the variables don’t get expanded inside single-quoted strings.
This might work:
sed -i "s~${searchString}1${replacementString}~g" $file
Really though, it’ll be easier to understand like this:
~ $ cat foo
<property name="sourceUrl" value="someurl?param=val"></property>
~ $ searchString='(<property *name="sourceUrl" *value=")[^?]*'
~ $ replacementString='file:///tmp/abc-efg'
~ $ sed -e "s~${searchString}~1${replacementString}~g" foo
<property name="sourceUrl" value="file:///tmp/abc-efg?param=val"></property>