[基本上,我需要使用读取以下示例内容的bash脚本,通过将子节点的字段嵌入XML树中的父节点来重新格式化自动生成的文件:
<app name="somename" location="somelocation">
<context folder="somefolder">
<stanza type="somestanza">
<setting name1="somesetting"/>
<setting name2="true"/>
<setting name3="someothersetting"/>
</stanza>
</context>
</app>
<app name="somename2" location="somelocation2" >
<context folder="somefolder">
<stanza type="somestanza">
<setting name1="somesetting"/>
<setting name2="true"/>
<setting name3="someothersetting"/>
<setting name4="moresettings"/>
</stanza>
</context>
</app>
使用grep,sed或awk,是否有办法将设置“ name3”拉入节的父节点?应该看起来像这样:
<app name="somename" location="somelocation" >
<context folder="somefolder">
<stanza type="somestanza" name3="someothersetting">
<setting name1="somesetting"/>
<setting name2="true"/>
<setting name3="someothersetting"/>
</stanza>
</context>
</app>
<app name="somename2" location="somelocation2" >
<context folder="somefolder">
<stanza type="somestanza" name3="someothersetting">
<setting name1="somesetting"/>
<setting name2="false"/>
<setting name3="someothersetting"/>
<setting name4="moresettings"/>
</stanza>
</context>
</app>
我看到一条路径,并且已经进行了一段时间,但最终似乎还有些遥远。任何帮助都会很棒!
更多背景:我们有一个脚本,该脚本读取具有键值对的数百个配置文件,并将其合并为单个xml文件,以供数据分析工具提取。我们正在尝试将name3提升到节节点。
<apps>
和</apps>
,并且假设属性name3
的值始终为"someothersetting"
,则可以尝试:xmlstarlet edit --insert "//stanza" --type attr --name "name3" --value "someothersetting" app.xml