How to insert subnode in xml file using xmlstarlet or any other bash command?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to insert subnode in xml file using xmlstarlet or any other bash command?
# 1  
Old 09-06-2019
How to insert subnode in xml file using xmlstarlet or any other bash command?

I have multiple xml files where i want to update a subnode if the subnode project points to different project or insert a subnode if it doesn't exist using a xmlstarlet or any other command that can be used in a bash script.

I have been able to update the subnode project if it doesn't point to the right project using xml starlet command, but i am not able to insert subnode project if the project tag doesn't exist.

I have tried.

Code:
xmlstarlet ed -P --update "///module[@id='pxc110237_1']/@project" -v"X1" --insert"///module[@id='pxc110237_1']/@project --type attr -n style -v"X1" file.xml

The file looks like:
Code:
 <?xml version="1.0"?>
  <kgcontent>
    <containers>
       <module project="ANY" level="1" id="pxc110233_1"/>
       <module project="X1" level="1" id="pxc110234_1"/>
       <module id="pxc110235_1"/>
    </containers>
  <kgcontent>

I want the output to look like:
Code:
<?xml version="1.0"?>
  <kgcontent>
    <containers>
       <module project="ANY" level="1" id="pxc110233_1"/>
       <module project="X1" level="1" id="pxc110234_1"/>
       <module project="X1" id="pxc110235_1"/>
    </containers>
  <kgcontent>

The starlet command i used doesn't have any effect on the file. how can i fix the above problem? any pointers would be great help Smilie

Linux distrubution: SUSE
Shell: Bash

Thank you!
This User Gave Thanks to Sekhar419 For This Post:
# 2  
Old 09-08-2019
You can easily use sed to accomplish this, if I understand your question correctly.

I suggest you try to use sed to accomplish this and post the code you wrote / tried and the results.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert a string and variable at specified position in command in bash?

I currently have a loop that reads all .bam files in a directory (wont always be 4 like in this example, into $id. What I am trying to do, unsucessfully, is create specific new lines in an exsisting command using each $id. Each new line would be: --bam ${id} \ Tried p=$dir... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. UNIX for Beginners Questions & Answers

Bash: Insert in a variable a file

hi all i have a problem in the bash shell. i'd like insert in a variable a file for example : i have a file datafine.log in this file there is : 17/JUN/2019 i want to insert the value of datafine.log in a variable. Regards Frncesco edit by bakunin: please use CODE-tags for your data... (2 Replies)
Discussion started by: Francesco_IT
2 Replies

3. Shell Programming and Scripting

Insert text after match in XML file

Having a little trouble getting this to work just right. I have xml files that i want to split some data. I have 2 <name> tags within the file I would like to take only the first tag and split the data. tag example. From this. TAB<Name>smith, john</Name> to TAB<Name>smith,... (8 Replies)
Discussion started by: whegra
8 Replies

4. Shell Programming and Scripting

awk command to insert a tag in XML

Hi All, I need a help on inserting a XML tag. Actual input <var> <nam>abcd</nam> <a1>.</a1> </var> if tag <a1>.</a1> is getting missed in XML like below <var> <nam>abcd</nam> </var> i need to insert wherever it is missed after <nam> tag and before </var> tag. Could anyone... (3 Replies)
Discussion started by: mohanalakshmi
3 Replies

5. Shell Programming and Scripting

Parsing XML (and insert data) then output data (bash / Solaris)

Hi folks I have a script I wrote that basically parses a bunch of config and xml files works out were to add in the new content then spits out the data into a new file. It all works - apart from the xml and config file format in the new file with XML files the original XML (that ends up in... (2 Replies)
Discussion started by: dfinch
2 Replies

6. Shell Programming and Scripting

Insert a new subnode in a xml file

Hi, i have an xml file and i want to edit a new sub node in a file like val="<activity android:label="@string/app_name" android_name=".MainActivity1" android:launchMode="singleTask" android:screenOrientation="portrait" ... (1 Reply)
Discussion started by: gautamshrm3
1 Replies

7. Shell Programming and Scripting

Insert value of env variable in xml file

Hello, I have the following variables set in my env echo $MY_XSD_FILE /home/jak/sample.xsd echo $MY_INTERVAL_VALUE 4 I want to insert them between the xml tags in my xml file cat sample.xml ::::::::::::::: ::::::::::::::: <property name="FILE"></property> :::::::::::::::::::::::... (2 Replies)
Discussion started by: jakSun8
2 Replies

8. Shell Programming and Scripting

xmlstarlet parse field from file

I have a xmlfile like this: <?xml version="1.0" encoding="utf-8"?> <contentlocation xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="http://wherein.yahooapis.com/v1/schema" xml:lang="en"> <processingTime>0.001538</processingTime> ... (1 Reply)
Discussion started by: unclecameron
1 Replies

9. Shell Programming and Scripting

xmlstarlet template parse small xml file

I have a file like: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <geonames> <geoname> <toponymName>Palos Verdes</toponymName> <name>Palos Verdes</name> <lat>42.1628912</lat> <lng>-123.6481235</lng> <geonameId>5718340</geonameId> <countryCode>US</countryCode>... (4 Replies)
Discussion started by: unclecameron
4 Replies

10. Shell Programming and Scripting

script to run shell command and insert results to existing xml file

Hi. Thanks for any help with this. I'm not new to programming but I am new to shell programming. I need a script that will 1. execute 'df -k' and return the volume names with specific text 2. surround each line of the above results in opening and closing xml tags 3. insert the results of step... (5 Replies)
Discussion started by: littlejon
5 Replies
Login or Register to Ask a Question