1

I would like to replace string in file contains ${user.home} using sed in Linux but I'm unable to do it. I tried below options using sed but failed.

The input file:

<property name="dev.home"  value="${user.home}"/>

Tried code to replace ${user.home}:

sed -i "s/$${user.home}/r_str/g" 1.xml
sed -i "s/${user.home}/r_str/g" 1.xml
sed -i "s/\$\{user\.home\}/r_str/g" 1.xml

Actual:

<property name="dev.home"  value="${user.home}"/>

Expected:

<property name="dev.home"  value="/dev"/>
halfer
  • 19,824
  • 17
  • 99
  • 186
Jitesh Sojitra
  • 3,655
  • 7
  • 27
  • 46

2 Answers2

2

You don't need to escape the curly braces {}

echo '${user.home}' | sed "s/\${user.home}/lol$/"
lol$
activedecay
  • 10,129
  • 5
  • 47
  • 71
2

Find and update attribute with xmlstarlet:

xmlstarlet edit --omit-decl --update 'property[@value="${user.home}"]/@value' --value '/dev' file

Output:

<property name="dev.home" value="/dev"/>
Cyrus
  • 84,225
  • 14
  • 89
  • 153