splitting a file (xml) into multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting splitting a file (xml) into multiple files
# 1  
Old 07-24-2010
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
---------

Code:
<?xml version="UTF_8">
<emp: ....>
 <name>a</name>
 <age>10</age>
</emp>
<?xml version="UTF_8">
<emp: ....>
 <name>b</name>
 <age>10</age>
</emp>
<?xml version="UTF_8">
<emp: ....>
 <name>c</name>
 <age>10</age>
</emp>

I want to split the test.xml into 3 files (each xml) like below

test1.xml
---------
Code:
<?xml version="UTF_8">
<emp: ....>
 <name>a</name>
 <age>10</age>
</emp>

test2.xml
---------
Code:
<?xml version="UTF_8">
<emp: ....>
 <name>b</name>
 <age>10</age>
</emp>

test3.xml
---------
Code:
<?xml version="UTF_8">
<emp: ....>
 <name>c</name>
 <age>10</age>
</emp>

I tried with the awk command but still didn't get thru.
Pls help on this.

Thanks,

Last edited by vgersh99; 07-24-2010 at 11:41 AM.. Reason: code tags, please!
# 2  
Old 07-24-2010
what exactly have you tried?
# 3  
Old 07-24-2010
Code:
awk '/<?xml version="UTF_8">/{n++}{print > f n}' f=test test.xml

Then even i modified the xml to include BEGINXML and ENDXML (for each xml wise) and tried with the below command

Code:
awk '/BEGINXML/{f="doc."++d} f{print > f} /ENDXML/{close f; f=""}' test.xml

its not working.

Pls suggest

Last edited by radoulov; 07-24-2010 at 02:39 PM.. Reason: Code tags, please!
# 4  
Old 07-24-2010
Hi,

try:

Code:
awk '/xml/{c++}{print > "file" c ".xml"}' file

HTH

Chris
# 5  
Old 07-24-2010
try and adapt the following awk script :
Code:
awk '
FNR==1 {
   path = namex = FILENAME;
   sub(/^.*\//,   "", namex);
   sub(namex "$", "", path );
   name = ext  = namex;
   sub(/\.[^.]*$/, "", name);
   sub("^" name,   "", ext );
}
/<\?xml / {
   if (out) close(out);
   out = path name (++file) ext ;
   print "Spliting to " out " ...";
}
/<\?xml /,/<\/emp>/ {
   print $0 > out
}
' sasi.xml

Input file (sasi.xml)
Code:
$ cat sasi.xml
<?xml version="UTF_8">
<emp: ....>
 <name>a</name>
 <age>10</age>
</emp>
<?xml version="UTF_8">
<emp: ....>
 <name>b</name>
 <age>10</age>
</emp>
<?xml version="UTF_8">
<emp: ....>
 <name>c</name>
 <age>10</age>
</emp>
$ ./sasi.sh
Spliting to sasi1.xml ...
Spliting to sasi2.xml ...
Spliting to sasi3.xml ...
$ more -999 sasi[0-9].xml
::::::::::::::
sasi1.xml
::::::::::::::
<?xml version="UTF_8">
<emp: ....>
 <name>a</name>
 <age>10</age>
</emp>
::::::::::::::
sasi2.xml
::::::::::::::
<?xml version="UTF_8">
<emp: ....>
 <name>b</name>
 <age>10</age>
</emp>
::::::::::::::
sasi3.xml
::::::::::::::
<?xml version="UTF_8">
<emp: ....>
 <name>c</name>
 <age>10</age>
</emp>
$

Jean-Pierre.
# 6  
Old 07-24-2010
By the way, your document declaration is invalid
Code:
<?xml version="UTF_8">

The version should be either 1.0 or 1.1. There is no valid XML version called "UTF_8". UTF-8 is a character encoding scheme.

The following is probably what you want:
Code:
<?xml version="1.0" encoding="UTF-8"?>

# 7  
Old 07-24-2010
MySQL

Code:
# cat 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>
</emp>
<?xml version="UTF_8">
<emp: ....>
 <name>c</name>
 <age>10</age>
</emp>

Code:
# ./justdoit Test.xml
 
1. Splitted File Name  -> "test1.xml"
<?xml version="UTF_8">
<emp: ....>
 <name>a</name>
 <age>10</age>
</emp>
 
2. Splitted File Name  -> "test2.xml"
<?xml version="UTF_8">
<emp: ....>
 <name>b</name>
 <age>10</age>
</emp>
 
3. Splitted File Name  -> "test3.xml"
<?xml version="UTF_8">
<emp: ....>
 <name>c</name>
 <age>10</age>
</emp>

Code:
 
# cat justdoit
 
#!/bin/bash
totalcnt=$(sed -n '/<?xml/,/emp>/p' $1 | sed -n '$=')
mycnt=$(sed -n '1,/emp>/p' $1 | sed -n '$=')
count=`expr $totalcnt / $mycnt `
first=1;endof=$mycnt;in=1
 
  while [ $(( count -=1 )) -gt -1 ]
   do
       sed -n "${first},${endof}p" $1 > test"$in"
       echo -e "\n$in. Splitted File Name  -> \"test"$in".xml"\" ; cat test"$in"
       first=`expr $first + $mycnt `
       endof=`expr $endof + $mycnt `
       in=`expr $in + 1 `
   done

Regards
ygemici

Last edited by ygemici; 07-24-2010 at 07:02 PM..
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 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

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

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

6. Shell Programming and Scripting

Splitting a single file to multiple files

Hi Friends , Please guide me with the code to extract multiple files from one file . The File Looks like ( Suppose a file has 2 tables list ,column length may vary ) H..- > File Header.... H....- >Table 1 Header.... D....- > Table 1 Data.... T....- >Table 1 Trailer.... H..-> Table 2... (1 Reply)
Discussion started by: AspiringD
1 Replies

7. Shell Programming and Scripting

Splitting XML file on basis of line number into multiple file

Hi All, I have more than half million lines of XML file , wanted to split in four files in a such a way that top 7 lines should be present in each file on top and bottom line of should be present in each file at bottom. from the 8th line actual record starts and each record contains 15 lines... (14 Replies)
Discussion started by: ajju
14 Replies

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

9. Shell Programming and Scripting

help splitting a file into multiple files in bash

I have a file that logs multiple sessions. What I would like to do is split this file inclusive of the lines that include "starting session" and "shutting down" and ignore the data before and after the beginning of the first session and the end of the last session. The output files can be called... (2 Replies)
Discussion started by: elinenbe
2 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