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



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

i have 2 license xml files: a-license.xml , b-license.xml. format of both license files same. want merge them single file.

sample input:

file a-license.xml contains

<company-license> <generator></generator> <customer></customer> <orderid></orderid> <expiration></expiration> <license> <module>a</module> <license_key>xxxx-xxxx-xxxxx</license_key> </license> </company-license>

file b-license.xml contains

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

desired output should like

<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>

i want extract <license> tag a-license.xml , append below <license> tag of b-license.xml.

how can this?

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

or shorter:

sed -n '1,/<\/license>/p' a-license.xml > new-license.xml sed -n '/<license>/,$p' b-license.xml >> new-license.xml

output file 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>

xml shell awk sed xmllint

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -