如何替换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屋!
查看全文