How replace variable names in an xml-file?

后端 未结 3 861
执笔经年
执笔经年 2020-12-20 05:56

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.informi         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-20 06:15

    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
    

提交回复
热议问题