3

My file contains a line like this:

<virtual-server name="default-host" enable-welcome-root="false">

I want to replace: enable-welcome-root from false to true

Using sed -n 's/.*enable-welcome-root="\([^"]*\).*/\1/p' file I can get the value which is false, but how can I replace it?

gogasca
  • 9,283
  • 6
  • 80
  • 125

3 Answers3

5

This would change from false or true to true

sed -r 's/(enable-welcome-root=")[^"]+"/\1true"/' file
<virtual-server name="default-host" enable-welcome-root="true">

or without -r

sed 's/\(enable-welcome-root="\)[^"]+"/\1true"/'
<virtual-server name="default-host" enable-welcome-root="false">

Use -i to write back to file

Jotne
  • 40,548
  • 12
  • 51
  • 55
3

Your string contains no special characters. Thus:

s='<virtual-server name="default-host" enable-welcome-root="false">'
r='<virtual-server name="default-host" enable-welcome-root="true">'
sed -i -e "s/$s/$r/" file
Cyrus
  • 84,225
  • 14
  • 89
  • 153
3

Using xmlstarlet, and adding a close tag to your line:

xmlstarlet ed -O -u '//virtual-server/@enable-welcome-root[.="false"]' -v "true" <<END
<virtual-server name="default-host" enable-welcome-root="false">
</virtual-server>
END
<virtual-server name="default-host" enable-welcome-root="true">
</virtual-server>
glenn jackman
  • 238,783
  • 38
  • 220
  • 352