Splitting the XML file and renaming the files

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Splitting the XML file and renaming the files
# 1  
Old 03-27-2018
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
Code:
 <jms-system-resource>
    <name>PS6SOAJMSModule</name>
    <target>soa_server1</target>
    <sub-deployment>
      <name>PS6SOASubDeployment</name>
      <target>PS6SOAJMSServer</target>
    </sub-deployment>
    <descriptor-file-name>jms/soajmsmodule-ps6-jms.xml</descriptor-file-name>
  </jms-system-resource>
  <jms-system-resource>
    <name>UMSJMSSystemResource</name>
    <target>soa_server1,bam_server1</target>
    <sub-deployment>
      <name>UMSJMSServer522129776</name>
      <target>UMSJMSServer_auto_1</target>
    </sub-deployment>
    <sub-deployment>
      <name>UMSJMSServer1709690790</name>
      <target>UMSJMSServer_auto_2</target>
    </sub-deployment>
    <descriptor-file-name>jms/UMSJMSSystemResource-jms.xml</descriptor-file-name>
  </jms-system-resource>

I would like to have above file splitted into two different files

File1 Should be named as JMSSSYSTEMRESOURSE1.XML and it needs to have the xml data like below
Code:
 <jms-system-resource>
    <name>PS6SOAJMSModule</name>
    <target>soa_server1</target>
    <sub-deployment>
      <name>PS6SOASubDeployment</name>
      <target>PS6SOAJMSServer</target>
    </sub-deployment>
    <descriptor-file-name>jms/soajmsmodule-ps6-jms.xml</descriptor-file-name>
  </jms-system-resource>

Second file should be named as JMSSSYSTEMRESOURSE2.XML and it needs to have the xml data like below
Code:
<jms-system-resource>
    <name>UMSJMSSystemResource</name>
    <target>soa_server1,bam_server1</target>
    <sub-deployment>
      <name>UMSJMSServer522129776</name>
      <target>UMSJMSServer_auto_1</target>
    </sub-deployment>
    <sub-deployment>
      <name>UMSJMSServer1709690790</name>
      <target>UMSJMSServer_auto_2</target>
    </sub-deployment>
    <descriptor-file-name>jms/UMSJMSSystemResource-jms.xml</descriptor-file-name>
  </jms-system-resource>

Can you please help me with it?
# 2  
Old 03-27-2018
One might think that the help you got with your other three threads would give you everything you need to solve this problem.

What operating system are you using?

What shell are you using?

What have you tried to solve this problem on your own?
# 3  
Old 03-28-2018
Hello Don,

I have tried below one code to extract all the
Code:
<jms-system-resource>

Code which i have tried
Code:
awk '
/<jms-system-resource>/{
  flag=1
  val=""
}
/<\/jms-system-resource>/{
  print val RS $0;
  val=flag="";
  next
}
flag && NF{
  val=val?val ORS $0:$0
}
'Inupt.xml

but the output is not promising for next transaction. Hence i have to generate separate files for each
Code:
<jms-system-resource>

tag.
And here are operating system details.

GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

Thanks,

---------- Post updated 03-28-18 at 11:25 AM ---------- Previous update was 03-27-18 at 02:53 PM ----------

I have also tried below and i think i am successfull using below
Code:
awk '/<jms-system-resource>/{c++}{print > "JMSSYSTEMRESOURCE_" c ".xml"}
' Test_data.xml

Thanks For your HelpSmilie
This User Gave Thanks to Siv51427882 For This Post:
# 4  
Old 03-28-2018
Quote:
Originally Posted by Siv51427882
... ... ...

I have also tried below and i think i am successfull using below
Code:
awk '/<jms-system-resource>/{c++}{print > "JMSSYSTEMRESOURCE_" c ".xml"}
' Test_data.xml

Thanks For your HelpSmilie
Congratulations!

Note, however, that the standards don't specify the precedence between string concatenation and output redirection. To be portable to all versions of awk, add parentheses around the concatenation as in:
Code:
awk '/<jms-system-resource>/{c++}{print > ("JMSSYSTEMRESOURCE_" c ".xml")}
' Test_data.xml

With the code you are using, you get the following diagnostic error (and no output files) when it is run using a BSD based version of awk (such as that found on macOS):
Code:
awk: syntax error at source line 1
 context is
	/<jms-system-resource>/{c++}{print > "JMSSYSTEMRESOURCE_" >>>  c <<<  ".xml"}
awk: illegal statement at source line 1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Splitting file into multiple files and renaming them

Hi all, Newbie here. First of all, sorry if I made any mistakes while posting this question in terms of rules. Correct me if I am wrong. :b: I have a .dat file whose name is in the format of 20170311_abc_xyz.dat. The file consists of records whose first column consists of multiple dates in... (2 Replies)
Discussion started by: chanduris
2 Replies

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

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

5. Shell Programming and Scripting

XML Splitting into multi files

Hi , I have a XML file like below file name : sample.xml <?xml version="1.0"?> <catalog> <author>Rajini</author> <title>XML Guide</title> <Text> </Text> <genre>Computer</genre> <price>44.95</price> </catalog> <?xml version="1.0"?> <catalog> ... (5 Replies)
Discussion started by: karthinvk
5 Replies

6. UNIX for Dummies Questions & Answers

[Solved] File Splitting And Renaming Problem

OK So I Recently Bought A whatbox Seed-box Act!!:cool: I am connected to whatbox via SSH!!! Now i have downloaded a movie and renamed it to 2yify.mp4 (800MB):o When I TYPE the command to split it which is:) split -b 400m 2yify.mp4 It gets renamed into two parts with different names... (4 Replies)
Discussion started by: anime12345
4 Replies

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

8. Shell Programming and Scripting

Splitting large file and renaming based on field

I am trying to update an older program on a small cluster. It uses individual files to send jobs to each node. However the newer database comes as one large file, containing over 10,000 records. I therefore need to split this file. It looks like this: HMMER3/b NAME 1-cysPrx_C ACC ... (2 Replies)
Discussion started by: fozrun
2 Replies

9. Shell Programming and Scripting

splitting a file (xml) into multiple files

To split the files Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Test.xml --------- <?xml version="UTF_8"> <emp: ....> <name>a</name> <age>10</age> </emp> <?xml version="UTF_8"> <emp: ....> <name>b</name> <age>10</age>... (11 Replies)
Discussion started by: sasi_u
11 Replies

10. Shell Programming and Scripting

splitting huge xml into multiple files

hi all i have a some huge html files (500MB to 1GB). Each file has multiple <html></html> tags <html> ................. .................... .................... </html> <html> ................. .................... .................... </html> <html> .................... (5 Replies)
Discussion started by: uttamhoode
5 Replies
Login or Register to Ask a Question