我有一个 XML 文件 abc.xml,我想使用 bash 脚本对其进行配置。例如,如果我有这个 abc.xml:
<a>
<b>
<bb>
<yyy>
Bla
</yyy>
</bb>
</b>
<c>
<cc>
Something
</cc>
</c>
<d>
bla
</d>
</a>
另一个 xyz.xml 文件
<xxx>
<yyy>
<ggg>
<hhh>true</hhh>
<kkk>always</kkk>
<mmm>warn</mmm>
</ggg>
<id>repo</id>
<url>https://repository/repo-2.0/</url>
<layout>default</layout>
</yyy>
</xxx>
在 abc.xml 文件中的标记之后</b>
和之前<c>
。我需要在文件 xyz.xml 中插入所有标签。我是 shell 脚本的新手。有人可以帮忙吗?谢谢!
如果Bla
或Something
或其他标签等包括<c>
:
$ awk 'NR==FNR { # process xyz
b=b (b==""?"":ORS) $0 # buffer its lines
next
}
match($0,/ *<c>/) { # look for <c>
print substr($0,1,RSTART-1) # print up to it
print b # print buffer
print substr($0,RSTART) # print from tag on
next
}1' xyz abc
编辑:小 awk 入门:
在这种情况下NR==FNR{...;next}
,在块中处理第一个给定的文件xyz
。ORS
是输出记录分隔符,即。默认为\n
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句