Issue splitting file based on XML tags


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue splitting file based on XML tags
# 1  
Old 01-19-2017
RedHat Issue splitting file based on XML tags

more a-d.txt1
Code:
<a-dets>
<a-serv>
<aserv>mymac14,mymac15:MYAPP:mydom:/web/domain/mydom/config
<NMGR>:MYAPP:/web/bea_apps/perf/NMGR/NMGR1034
<a-rep-string>
11.12.10.01=192.10.00.26
10.20.18.10=192.10.00.27
</a-rep-string>
</a-serv>
<w-serv>
<wserv>mywmac3,mywmac4:MYAPP:dev.MYAPP01.mycomp.com:/web/apache/2.4.10/https-MYAPP01/conf
<web-replace-string>
</web-replace-string>
</w-serv>
</a-dets>

I need to save the contents of first occurrence of <a-dets></a-dets> in app1.tmp and the next occurrence if found in the app2.tmp and so on.

It used to work on my previous Operating System using the below command.

Code:
awk '/<a-dets>/{A=1;++i} A{print >> ("app"i".tmp")} /<\/a-dets>/{A=0} ' a-d.txt1

But it does not work on my current OS i.e Linux mymac 2.6.18-416.el5 #1 SMP Wed Oct 26 12:04:18 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux

Question:

1. Can you please help explain what went wrong and why is the awk not extracting the data between the tags ? Is that acceptable or fault in the command ?

2. Can i use sed instead of awk to achieve the same as awk has too many flavors to deal with and i want this to run on most OS.

Last edited by mohtashims; 01-19-2017 at 09:52 AM..
# 2  
Old 01-19-2017
Hello mohtashims,

There could be 2 possibilities.
I- If you have used above shown sample Input_file only, then obviously it will show 1 app file only as I could see only occurrence of <a-dets>, so you could try with Input_file which is having 2 or more occurrences of it.
II- You could a little change to your code(though not tested it) as follows.
Code:
awk '/<a-dets>/{A=1;++i} A{print >> ("app"i".tmp")} /<\/a-dets>/{A=""}'  Input_file

Highlighted bold part only I have changed in above code, both are working fine for me, kindly do let me know on same, how it goes.

Thanks,
R. Singh
# 3  
Old 01-19-2017
Again and again and again: WHAT "does not work", and how?

What you posted works pretty well on my linux system.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting the XML file and renaming the files

Hello Gurus, I have a requirement to split the xml file into different xml files. Can you please help me with that? Here is my Source XML file <jms-system-resource> <name>PS6SOAJMSModule</name> <target>soa_server1</target> <sub-deployment> ... (3 Replies)
Discussion started by: Siv51427882
3 Replies

2. Shell Programming and Scripting

Splitting the XML file into three different files

Hello Shell Guru's I have a requirement to split the source xml file into three different text file. And i need your valuable suggestion to finish this. Here is my source xml snippet, here i am using only one entry of <jms-system-resource>. There may be multiple entries in the source file. ... (5 Replies)
Discussion started by: Siv51427882
5 Replies

3. Shell Programming and Scripting

Splitting CSV into variables then to XML file

I have a text file that looks like this: FIELD1, FIELD2, THIS IS FIELD3, FIELD4 FIELD1, FIELD2, THIS IS FIELD3, FIELD4 FIELD1, FIELD2, THIS IS FIELD3, FIELD4 I need it to turn it into an XML file to run against a custom application. My ultimate goal is for it to look like... (15 Replies)
Discussion started by: jeffs42885
15 Replies

4. Shell Programming and Scripting

Splitting a single xml file into multiple xml files

Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Sample.xml consists multiple headers so how can we split these multiple headers into multiple files in unix. eg : <?xml version="1.0" encoding="UTF-8"?> <ml:individual... (3 Replies)
Discussion started by: Narendra921631
3 Replies

5. Shell Programming and Scripting

Split xml file into multiple xml based on letterID

Hi All, We need to split a large xml into multiple valid xml with same header(2lines) and footer(last line) for N number of letterId. In the example below we have first 2 lines as header and last line as footer.(They need to be in each split xml file) Header: <?xml version="1.0"... (5 Replies)
Discussion started by: vx04
5 Replies

6. Shell Programming and Scripting

Splitting xml file into several xml files using perl

Hi Everyone, I'm new here and I was checking this old post: /shell-programming-and-scripting/180669-splitting-file-into-several-smaller-files-using-perl.html (cannot paste link because of lack of points) I need to do something like this but understand very little of perl. I also check... (4 Replies)
Discussion started by: mcosta
4 Replies

7. Shell Programming and Scripting

Help with Splitting a Large XML file based on size AND tags

Hi All, This is my first post here. Hoping to share and gain knowledge from this great forum !!!! I've scanned this forum before posting my problem here, but I'm afraid I couldn't find any thread that addresses this exact problem. I'm trying to split a large XML file (with multiple tag... (7 Replies)
Discussion started by: Aviktheory11
7 Replies

8. Shell Programming and Scripting

Split XML file based on tags

Hello All , Please help me with below requirement I want to split a xml file based on tag.here is the file format <data-set> some-information </data-set> <data-set1> some-information </data-set1> <data-set2> some-information </data-set2> I want to split the above file into 3... (5 Replies)
Discussion started by: Pratik4891
5 Replies

9. Shell Programming and Scripting

Help required in Splitting a xml file into multiple and appending it in another .xml file

HI All, I have to split a xml file into multiple xml files and append it in another .xml file. for example below is a sample xml and using shell script i have to split it into three xml files and append all the three xmls in a .xml file. Can some one help plz. eg: <?xml version="1.0"?>... (4 Replies)
Discussion started by: ganesan kulasek
4 Replies

10. UNIX for Dummies Questions & Answers

Extract a specific number from an XML file based on the start and end tags

Hello People, I have the following contents in an XML file ........... ........... .......... ........... <Details = "Sample Details"> <Name>Bob</Name> <Age>34</Age> <Address>CA</Address> <ContactNumber>1234</ContactNumber> </Details> ........... ............. .............. (4 Replies)
Discussion started by: sushant172
4 Replies
Login or Register to Ask a Question