繁体   English   中英

如何使用sed替换字符串中间的匹配字符

[英]How to use sed to replace the matching character in middle of string

我有一个XML文件“ datasources.xml”

<item-map group="org.base" datasource="localderby"/>
<item-map group="org.base.pos" datasource="localderbypos"/>
<item-map group="org.base.hr" datasource="localderbyhr"/>

并需要与此类似的输出

<item-map group="org.base" datasource="localpostgres"/>
<item-map group="org.base.pos" datasource="localpostpos"/>
<item-map group="org.base.hr" datasource="localposthr"/>

所以我已经通过命令产生了输出

sed -e's / datasource =“ localderby” / datasource =“ localpostgres” / g'-e's / datasource =“ localderby([az])*” / datasource =“ localpost * / g'datasources.xml

在第一个表达式中,我已经有了想要的“ localpostgres”,但是第二个表达式丢失了。 有人可以帮忙,请指教吗? 感谢转发。

使用扩展的正则表达式( sed -r )和+代替* ,并进行分组:

sed -r -e 's/datasource="localderby"/datasource="localpostgres"/g' -e 's/datasource="localderby([a-z]+)"/datasource="localpost\1"/g' datasources.xml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM