如何替换 xml 文件中的变量名? [英] How replace variable names in an xml-file?
本文介绍了如何替换 xml 文件中的变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
java 构建工具 ant 提供过滤器来用变量的值替换变量
The java build tool ant provides filter to replace variables by their values
示例:具有属性的文件:
Example: A file with properties:
db.user.name=user
db.driver=com.informix.jdbc.IfxDriver
具有通用设置的 XML 文件(注意 @variables@ )
A XML file with generic settings (Note the @variables@ )
<driver-class>@db.driver@</driver-class>
<user-name>@db.user.name@</user-name>
使用过滤器应对后变成
<driver-class>com.informix.jdbc.IfxDriver</driver-class>
<user-name>user</user-name>
如何使用 bash 和普通的 unix 工具实现此功能?
How can this functionallity be achieved with bash and plain unix tools?
推荐答案
这是另一个仅使用 bash 的实现.如果您可以根据需要使用 python 版本,我会建议您这样做.维护起来会更容易.否则你可以试试这个 bash 脚本:
This is an other implementation using bash only. If you can take the python version for you need I would suggest that. It will be easier to maintain. Otherwise you could try with this bash script:
#!/bin/bash
config="$1"
xml="$2"
tmp=$(mktemp)
cat "$config" | while read line; do
key=`echo $line | sed -n 's/^\([^=]\+\)=\(.*\)$/\1/p'`
value=`echo $line | sed -n 's/^\([^=]\+\)=\(.*\)$/\2/p'`
echo " sed 's/@$key@/$value/g' | " >> $tmp
done
replacement_cmd=`cat $tmp`
eval "cat \"$xml\" | $replacement_cmd cat"
rm -f $tmp
这篇关于如何替换 xml 文件中的变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文