2

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
  • 8,383
  • 5
  • 71
  • 114

3 Answers3

4

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
  • 39,326
  • 11
  • 49
  • 54
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
  • 223,850
  • 36
  • 205
  • 328
2

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
  • 77,979
  • 13
  • 71
  • 125