使用 Shell 脚本提取 XML 标记并附加到另一个 xml 中的特定位置

Extract XML Tag and append to a specific location in another xml using Shell Script

我有两个许可证 xml 文件:a-license.xmlb-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>

文件 b-license.xml 包含

  <company-license>  
   <generator></generator>
   <customer></customer>
   <orderid></orderid>
   <expiration></expiration>
   <license>
      <module>B</module>
      <license_key>yyyy-yyyy-yyyy</license_key>
   </license>
   </company-license>

所需的输出应该类似于

  <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-license.xml 中提取 <license> 标签并将其附加到 b-license.xml<license> 标签下方。

我该怎么做?

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 '1,/<\/license>/p' a-license.xml > new-license.xml
sed -n '/<license>/,$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>

$ awk 'NR==FNR{print;next} /<license>/{f=1} f' fileA fileB
<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>