我的文件包含如下一行:
<virtual-server name="default-host" enable-welcome-root="false">我想替换:启用-欢迎-根从假到真
使用sed -n 's/.*enable-welcome-root="\([^"]*\).*/\1/p' file,我可以得到值,即false,但如何替换它呢?
发布于 2015-03-28 06:28:51
这将从false或true更改为true
sed -r 's/(enable-welcome-root=")[^"]+"/\1true"/' file
<virtual-server name="default-host" enable-welcome-root="true">或者没有-r
sed 's/\(enable-welcome-root="\)[^"]+"/\1true"/'
<virtual-server name="default-host" enable-welcome-root="false">使用-i将文件写回
发布于 2015-03-28 13:06:19
使用xmlstarlet,并在行中添加一个关闭标记:
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>发布于 2015-03-28 07:11:49
您的字符串不包含特殊字符。因此:
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/" filehttps://stackoverflow.com/questions/29314307
复制相似问题