- 论坛徽章:
- 1
|
文本1里面内容是
id=1000
name=jack
ip=11.11.111.11
type=wsc
one=1
two=2
port=4444
aaa=1000
....
文本2里面内容是
<web-xml>
<conf id='1' type='int'>${ip}</conf>
<conf id='2' type='yy'>${name}</conf>
<conf id='3' type='int'>${aaa}</conf>
<conf id='4' type='int'>${id}</conf>
</web-xml>
<port>$port</port>
....
实现内容:
<web-xml>
<conf id='1' type='int'>11.11.111.11</conf>
<conf id='2' type='yy'>jack</conf>
<conf id='3' type='int'>1000</conf>
<conf id='4' type='int'>1000</conf>
</web-xml>
<port>4444</port>
本人用以下awk脚本实现,我要问的问题是#后面的
awk 'BEGIN{FS="="}
NR==FNR{
a["${"$1"}"]=$2
next
}
{
match($0,/\$\{[^}]+\}/)
c=substr($0,RSTART,RLENGTH)
sub(/\$\{[^}]+\}/,a[c],$0) #为啥用sub(c,a[c],$0)替换失败,然道sub(r, s [, t])中的r不能用变量?
}1' 1.txt 2.txt |
|