Source xml file splitter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Source xml file splitter
# 1  
Old 10-17-2012
Source xml file splitter

I have a source file that contains multiple XML files concatenated in it. The separator string between files is <?xml version="1.0" encoding="utf-8"?>. I wanted to split files in multiple files with mentioned names. I had used a awk code earlier to spilt files in number of lines i.e.
Code:
 awk 'NR%"'"${splitCount}"'"==1{x="'"${SrcFileName}_"'" sprintf("%04d",++i) ".txt"}{print > x}' $SrcFileName.txt

Not much aware of awk scripting hence dont know if we can use this code or there other better/ faster way of doing this. Below is example of source and expected target files.

Code:
Source File: XML_DUMP.txt
<?xml version="1.0" encoding="utf-8"?>
<XML1 data>
<?xml version="1.0" encoding="utf-8"?>
<XML2 data>
<?xml version="1.0" encoding="utf-8"?>
<XML3 data>
 
Target Files: 
File 1: XML_DUMP_0001.txt
<?xml version="1.0" encoding="utf-8"?>
<XML1 data>
 
File 2: XML_DUMP_0002.txt
<?xml version="1.0" encoding="utf-8"?>
<XML1 data>
 
File 3: XML_DUMP_0003.txt
<?xml version="1.0" encoding="utf-8"?>
<XML1 data>

# 2  
Old 10-17-2012
try

Code:
awk '/xml version="1.0"/{x="'"${SrcFileName}_"'" sprintf("%04d",++i) ".txt";}
 {print > x}' $SrcFileName.txt

This User Gave Thanks to pamu For This Post:
# 3  
Old 10-17-2012
Code:
awk '/<\?xml version="1\.0" encoding="utf-8"\?>/{n++}
n{f="XML_DUMP" sprintf("%04d",n) ".txt"
print >> f
close(f)}' XML_DUMP.txt

This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 10-17-2012
Thanks @pamu and @elixir

I am happy with below code except two things.
1> Need a variable in awk that can be assigned so shell parameter
2> was not able to assign <?xml version="1.0" encoding="utf-8"?> to a variable

Code:
existing working code 
awk '/xml version="1.0"/{x="'"${SrcFileName}_"'" sprintf("%04d",++i) ".txt";}  {print > x}' $SrcFileName.txt
 
need a 2 line code something like
word=<?xml version="1.0" encoding="utf-8"?>
 
awk ' $word {x="'"${SrcFileName}_"'" sprintf("%04d",++i) ".txt";}  {print > x}' $SrcFileName.txt

# 5  
Old 10-17-2012
Quote:
Originally Posted by santosh2k2
Thanks @pamu and @elixir

I am happy with below code except two things.
1> Need a variable in awk that can be assigned so shell parameter
2> was not able to assign <?xml version="1.0" encoding="utf-8"?> to a variable
try this...

Code:
word='<?xml version="1.0" encoding="utf-8"?>'
 
awk -v serch="$word" '{if($0 ~ serch){x="'"${SrcFileName}_"'" sprintf("%04d",++i) ".txt";print > x}else{print > x}}' $SrcFileName.txt

if above word assignment works properly then it's greatSmilie

if not try using below.(Not tested. away from machineSmilie)

Code:
word="<?xml version=\"1.0\" encoding=\"utf-8\"?>"

# 6  
Old 10-17-2012
@pamu.. word assignment is working fine...awk command s giving error as
Code:
awk: 0602-576 A print or getline function must have a file name.
 The input line number is 1. The file is a.txt.
 The source line number is 1.

# 7  
Old 10-17-2012
Quote:
Originally Posted by santosh2k2
@pamu.. word assignment is working fine...awk command s giving error as
Code:
awk: 0602-576 A print or getline function must have a file name.
 The input line number is 1. The file is a.txt.
 The source line number is 1.

This might be because of word is not getting properly in awk.

If you don't have any problem. minimize the word length.
Now checkSmilie

Code:
word="<?xml version="
 
awk -v serch="$word" '{if($0 ~ serch){x="'"${SrcFileName}_"'" sprintf("%04d",++i) ".txt";print > x}else{print > x}}' $SrcFileName.txt


Last edited by pamu; 10-17-2012 at 12:27 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

Grepping multiple XML tag results from XML file.

I want to write a one line script that outputs the result of multiple xml tags from a XML file. For example I have a XML file which has below XML tags in the file: <EMAIL>***</EMAIL> <CUSTOMER_ID>****</CUSTOMER_ID> <BRANDID>***</BRANDID> Now I want to grep the values of all these specified... (1 Reply)
Discussion started by: shubh752
1 Replies

2. Shell Programming and Scripting

Comparing delta values of one xml file in other xml file

Hi All, I have two xml files. One is having below input <NameValuePair> <name>Daemon</name> <value>tcp:7474</value> </NameValuePair> <NameValuePair> <name>Network</name> <value></value> </NameValuePair> ... (2 Replies)
Discussion started by: sharsour
2 Replies

3. Shell Programming and Scripting

File splitter

I have below script which does splitting based on a different criteria. can it be amended to produce required result SrcFileName=XML_DUMP awk '/<\?xml version="1\.0" encoding="utf-8"\?>/{n++} n{f="'"${SrcFileName}_"'" sprintf("%04d",n) ".txt" print >> f close(f)}' $SrcFileName.txt My... (3 Replies)
Discussion started by: santosh2k2
3 Replies

4. Shell Programming and Scripting

File Splitter output filename

Issue: I am able to split source file in multiple files of 10 rows each but unable to get the required outputfile name. please advise. Details: input = A.txt having 44 rows required output = A_001.txt , A_002.txt and so on. Can below awk be modified to give required result current... (19 Replies)
Discussion started by: santosh2k2
19 Replies

5. Shell Programming and Scripting

Text Splitter

Hi, I need to split files based on text: BEGIN DSJOB Identifier "LA" DateModified "2011-10-28" TimeModified "11.10.02" BEGIN DSRECORD Identifier "ROOT" BEGIN DSSUBRECORD Owner "APT" Name "RecordJobPerformanceData" Value "0" ... (16 Replies)
Discussion started by: unme
16 Replies

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

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

I need script to get xml access export from source server

Hi, I need a shell script to create to get XML access export from the source server. Thanks in Advance (3 Replies)
Discussion started by: bcb
3 Replies

9. Shell Programming and Scripting

File splitter by nth row

I need to split a file into n separate files of about the same size. The way the file will be split is at every nth row, starting with the first row, that row will be cut and copied to it's corresponding new file so that each file has unique records. Any 'leftovers' will go into the last file. e.g.... (4 Replies)
Discussion started by: sitney
4 Replies

10. Shell Programming and Scripting

Help needed in writing awk script for xml source

Hi, i am not able to get an approach for converting xml file to flat file using awk programming. Can anyone help me out. The input xml is like this: <outer> <field1>one</field1> <field2>two</field2> <field3>three<Error Code=777 Description=12345/></field3> <field4>four</field4> </outer>... (2 Replies)
Discussion started by: naren_0101bits
2 Replies
Login or Register to Ask a Question