我有这个文件config.xml
<widget id="com.example.hello" version="0.0.1">
<name>HelloWorld</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@callback.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<enter>PASSWORD</enter>
<content src="index.html" />
<access origin="*" />
我试着用sed做这件事,但没有成功。
我需要这样做:
$./script.sh config.xml NEWPASSWORD得到:
<widget id="com.example.hello" version="0.0.1">
<name>HelloWorld</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@callback.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<enter>NEWPASSWORD</enter>
<content src="index.html" />
<access origin="*" />
发布于 2016-11-11 13:40:49
使用反向引用:
sed "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"^\( *<enter>\):从任意数量的空格开始搜索行,然后是<enter>。使用转义括号捕获匹配字符。\([^>]*\)<:在第二个组中捕获下一个<上的字符。\1$2<:在替换字符串中,输出来自第一组的字符(\1),然后是传递给脚本的第二个参数值($2,新密码值)该命令应用于$1,该文件作为第一个参数传递给脚本(文件名)。
若要编辑文件的位置,请使用-i标志:
sed -i "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"发布于 2016-11-11 14:38:56
好的结果是:
$cat script.sh
#!/bin/sh
file=$1
sed -i "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"然后:
$./script.sh config.xml NEWPASSWORD感谢每个人,尤其是肯纳沃兹。
https://stackoverflow.com/questions/40548958
复制相似问题