使用Shell脚本提取XML标记并附加到另一个XML中的特定位置
我有两个许可证xml文件:使用Shell脚本提取XML标记并附加到另一个XML中的特定位置,xml,shell,awk,sed,xmllint,Xml,Shell,Awk,Sed,Xmllint,我有两个许可证xml文件:a-license.xml和b-license.xml。两个许可证文件的格式相同。我想把它们合并成一个文件 样本输入: 文件a-license.xml包含 <company-license> <generator></generator> <customer></customer> <orderid></orderid> <expiration>
a-license.xml
和b-license.xml
。两个许可证文件的格式相同。我想把它们合并成一个文件
样本输入:
文件a-license.xml包含
<company-license>
<generator></generator>
<customer></customer>
<orderid></orderid>
<expiration></expiration>
<license>
<module>A</module>
<license_key>xxxx-xxxx-xxxxx</license_key>
</license>
</company-license>
<company-license>
<generator></generator>
<customer></customer>
<orderid></orderid>
<expiration></expiration>
<license>
<module>B</module>
<license_key>yyyy-yyyy-yyyy</license_key>
</license>
</company-license>
A.
xxxx-xxxx-xxxxx
文件b-license.xml包含
<company-license>
<generator></generator>
<customer></customer>
<orderid></orderid>
<expiration></expiration>
<license>
<module>A</module>
<license_key>xxxx-xxxx-xxxxx</license_key>
</license>
</company-license>
<company-license>
<generator></generator>
<customer></customer>
<orderid></orderid>
<expiration></expiration>
<license>
<module>B</module>
<license_key>yyyy-yyyy-yyyy</license_key>
</license>
</company-license>
B
YYYYYYYYYY
期望的输出应该是
<company-license>
<generator></generator>
<customer></customer>
<orderid></orderid>
<expiration></expiration>
<license>
<module>A</module>
<license_key>xxxx-xxxx-xxxxx</license_key>
</license>
<license>
<module>B</module>
<license_key>yyyy-yyyy-yyyy</license_key>
</license>
</company-license>
A.
xxxx-xxxx-xxxxx
B
YYYYYYYYYY
我想从a-license.xml
中提取
标记,并将其附加到b-license.xml
的
标记下面
我如何才能做到这一点?sed-n'1,//{//d;p;}'a-license.xml>new-license.xml
sed -n '1,/<license>/{/<license>/d;p;}' a-license.xml > new-license.xml
sed -n '/<license>/,/<\/license>/p' a-license.xml >> new-license.xml
sed -n '/<license>/,/<\/company-license>/p' b-license.xml >> new-license.xml
sed-n'//,//p'a-license.xml>>new-license.xml
sed-n'//,//p'b-license.xml>>new-license.xml
或更短:
sed -n '1,/<\/license>/p' a-license.xml > new-license.xml
sed -n '/<license>/,$p' b-license.xml >> new-license.xml
sed-n'1,//p'a-license.xml>new-license.xml
sed-n'/,$p'b-license.xml>>new-license.xml
输出到文件new-license.xml:
<company-license>
<generator></generator>
<customer></customer>
<orderid></orderid>
<expiration></expiration>
<license>
<module>A</module>
<license_key>xxxx-xxxx-xxxxx</license_key>
</license>
<license>
<module>B</module>
<license_key>yyyy-yyyy-yyyy</license_key>
</license>
</company-license>
A.
xxxx-xxxx-xxxxx
B
YYYYYYYYYY
$awk'NR==FNR{print;next}/{f=1}f'fileA fileB
A.
xxxx-xxxx-xxxxx
B
YYYYYYYYYY
几乎可以肯定的是,您没有引用变量。尝试echo“$license\u tag”
而不是echo$license\u tag
。如果不是这样,请添加更多信息,说明您尝试了什么,tit以何种方式失败,以及我们可以测试解决方案的样本输入/预期输出。对于@Ed Morton造成的混淆,我们深表歉意。我刚刚更新了这个问题。这些记号真的存在于你的文件中吗?如果没有,则删除它们否。这是我第一次发布有关stackoverflow的问题,对于编写shell脚本也很陌生。感谢突出显示。我看到您修改了输入以包含更多行。这是一个非常重要的改变,从你原来的职位,使它成为一个完全不同的问题。在任何人花费时间试图帮助您解决新问题之前,现在真的想一想,您是否应该在示例中添加其他内容。很抱歉,我不熟悉Stackoverflow和使用xml解析的shell脚本。我为自己做了一些工作,但有一个小错误。
标记没有正确附加。我没有指定这一点以前。我已经更新了I/P和O/P文件。因此,当前我的O/P与您指定的命令类似:A xxxx-xxxx-xxxxx Byyyyyyyyyyy如果我能在中间删除<代码> <代码>,那就太棒了。它给出了一个错误<代码> SED:1:“1,//{/对不起,我编写了此代码用于GNU sed。我已通过answer进行了更新。这很有帮助。非常感谢。我再次为混淆感到抱歉,但该文件在顶部包含另一个标记
,它在
之后结束。类似于..
您指定的命令可以工作,但它不会附加到顺序正确。我不知道这意味着什么。输出与您最初请求的输出、顺序和所有内容完全相同。是的,我已编辑了我的问题。