awk command to insert a tag in XML


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command to insert a tag in XML
# 1  
Old 10-25-2013
awk command to insert a tag in XML

Hi All,

I need a help on inserting a XML tag.
Actual input
Code:
<var>
<nam>abcd</nam>
<a1>.</a1>
</var>

if tag
Code:
<a1>.</a1>

is getting missed in XML like below
Code:
<var>
<nam>abcd</nam>
</var>

i need to insert wherever it is missed after <nam> tag and before </var> tag.
Could anyone help me on this.Thank you

---------- Post updated at 08:43 AM ---------- Previous update was at 07:25 AM ----------

I tried the below code but gting syntax error at else part,
file.txt will be having the
Code:
<a1>.</a1>

Code:
 
awk -v f="file.txt" '{ if ($0!="<a1>") { while (getline < f) txt=txt $0} /<\/nam>/x sub("</var>",txt) { else print $0 } }1' input.xml

# 2  
Old 10-25-2013
Try this:
Code:
awk '/<nam>/ {L=1} /<a1>/ {L=0} /<\/var>/ && L {print "<a1>.</a1>"} 1' input.xml

This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-25-2013
Thanks a lot RudiC for great code. I was egarly waiting for the same post reply. Could you please explain it a bit, it will help us to improve our skills.


Thanks,
R. Singh
# 4  
Old 10-25-2013
Code:
awk     '/<nam>/                {L=1}                   # if a line contains <nam>, keep that fact in variable L
         /<a1>/                 {L=0}                   # if then <a1> occurs, no action necessary, reset L
         /<\/var>/ && L         {print "<a1>.</a1>"}    # if end of section and L still set, print missing string.
                                                        # you might want to reset L as well to be on the safe side
         1                                              # print normal lines from file
        ' file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to insert a CSV within xml element tag using Python?

Hi Team, I have a CSV file which I have to read through and needs to insert the content within an XML file using Python ONLY ( as most of the code base we have in python only). I managed to find the first part, missing how to insert to XML under "specific" tags. cat input.csv... (0 Replies)
Discussion started by: panyam
0 Replies

2. Shell Programming and Scripting

Moving XML tag/contents after specific XML tag within same file

Hi Forum. I have an XML file with the following requirement to move the <AdditionalAccountHolders> tag and its content right after the <accountHolderName> tag within the same file but I'm not sure how to accomplish this through a Unix script. Any feedback will be greatly appreciated. ... (19 Replies)
Discussion started by: pchang
19 Replies

3. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

4. Shell Programming and Scripting

XML Parse between to tag with upper tag

Hi Guys Here is my Input : <?xml version="1.0" encoding="UTF-8"?> <xn:MeContext id="01736"> <xn:VsDataContainer id="01736"> <xn:attributes> <xn:vsDataType>vsDataMeContext</xn:vsDataType> ... (12 Replies)
Discussion started by: pareshkp
12 Replies

5. Shell Programming and Scripting

Using shell command need to parse multiple nested tag value of a XML file

I have this XML file - <gp> <mms>1110012</mms> <tg>988</tg> <mm>LongTime</mm> <lv> <lkid>StartEle=ONE, Desti = Motion</lkid> <kk>12</kk> </lv> <lv> <lkid>StartEle=ONE, Source = Velocity</lkid> <kk>2</kk> </lv> <lv> ... (3 Replies)
Discussion started by: NeedASolution
3 Replies

6. Shell Programming and Scripting

awk Script to parse a XML tag

I have an XML tag like this: <property name="agent" value="/var/tmp/root/eclipse" /> Is there way using awk that i can get the value from the above tag. So the output should be: /var/tmp/root/eclipse Help will be appreciated. Regards, Adi (6 Replies)
Discussion started by: asirohi
6 Replies

7. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

8. Shell Programming and Scripting

How to retrieve the value from XML tag whose end tag is in next line

Hi All, Find the following code: <Universal>D38x82j1JJ </Universal> I want to retrieve the value of <Universal> tag as below: Please help me. (3 Replies)
Discussion started by: mjavalkar
3 Replies

9. Shell Programming and Scripting

shell command to remove some XML tag is needed

Hi all, I have a file which i have to remove some line from it, the lines that i have to remove from my file is as below: </new_name></w"s" langue="Fr-fr" version="1.0" encoding="UTF-8" ?> <New_name> and it is finding at the middle of my file, is there any command line in linux to do it or do... (10 Replies)
Discussion started by: id_2pc
10 Replies

10. Shell Programming and Scripting

Sed command to clean xml tag

Hi, Can someone help me come up with a generic sed command to clean a tag off its attributes? For eg. Input String - <tag attrib=new>This String</tag> should undergo a sed transformation to get Output String - <tag >This String</tag> This works - echo "<tag attrib=new>This</tag>" |... (3 Replies)
Discussion started by: iamwha1am
3 Replies
Login or Register to Ask a Question