Sponsored Content
Full Discussion: awk file split
Top Forums Shell Programming and Scripting awk file split Post 302885184 by f0usk4s on Friday 24th of January 2014 04:30:34 AM
Old 01-24-2014
awk file split

Hi all,

First of all I' like to mention that I'm pretty new to unix scripting. Smilie

I'm trying to split an large xml with awk and rename it based on the values of two attributes.
Example XML
Code:
<RECORD>
<element1>11</element1>
<element2>22</element2>
<element3>33</element3>
<element4>a</element4>
<element5>b</element5>
<element6>c</element6>
</RECORD>
<RECORD>
<element1>44</element1>
<element2>55</element2>
<element3>66</element3>
<element4>a</element4>
<element5>b</element5>
<element6>c</element6>
</RECORD>

The desired output would be :

Code:
a file named 1122.xml
<RECORD>
<element1>11</element1>
<element2>22</element2>
<element3>33</element3>
</RECORD>

a file named 4455.xml
<RECORD>
<element1>44</element1>
<element2>55</element2>
<element3>66</element3>
</RECORD>

Up till know I have been able to split the file using this command
Code:
awk '/<RECORD/{close("row"count".xml");count++}count{f="row"count".xml";print $0 > f}' file.xml

but couldn't figure how to pass the values of the elements 1 & 2 in the filename. Ideally I would like to add xml tags as this is intended to split xml files and produce valid xml format output.

Any ideas on how to do it with awk ?
Thank you in advance.

PS. I know that it has been addressed in a couple of posts, such as shell-210529-xml-split-extract-string-between-chars.html
though I could use the proposed solution, it always produce me only one file with the final record.Smilie

again, thanks
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split file using awk

I am trying to read a file and split the file into multiple files. I need to create new files with different set of lines from the original file. ie, the first output file may contain 10 lines and the second 100 lines and so on. The criteria is to get the lines between two lines starting with some... (8 Replies)
Discussion started by: pvar
8 Replies

2. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

3. Shell Programming and Scripting

split file with awk

I did a lot of search on this forum on spiting file; found a lot, but my requirement is a bit different, please guide. Master file: x:start:5 line1:23 line2:12 2:90 x:end:5 x:start:2 45:56 22:90 x:end:2 x:start:3 line1:23 line2:12 x:end:3 x:start:2 line5:23 (1 Reply)
Discussion started by: uwork72
1 Replies

4. Shell Programming and Scripting

awk - split file

How can I split a text file (in awk) in n others with number of record given in input? Thanks (6 Replies)
Discussion started by: pinguc
6 Replies

5. Shell Programming and Scripting

split a file using awk

Hi , I just need to split a file and outputfiles are redirected to gzip file need: Input file - A.gz content of A.gz is 100|sfdds|dffdds|200112|sdfdf 100|sfdds|dffdds|200112|sdfdf 100|sfdds|dffdds|200112|sdfdf 100|sfdds|dffdds|200212|sdfdf 100|sfdds|dffdds|200212|sdfdf... (3 Replies)
Discussion started by: mohan_xunil
3 Replies

6. Shell Programming and Scripting

How to split a file using AWK?

Hello, I have a file like the following: david,a,b,c,20,r thomas,a,b,c,30,r willaiam,a,b,c,80,r barbara,a,b,c,100,r I would like to split the file into other files using a condition for the contents of column 5. The condition should be a if the contents of column 5 is in a range... (4 Replies)
Discussion started by: keenboy100
4 Replies

7. Shell Programming and Scripting

AWK File Split

Hi All, Input.txt XYZONEABC                  CZXTWOJJJ KKKSIXOOO asdfhajlsdhfajs asdfasfasdf Output Files: ONE.txt XYZONEABC                 TWO.txt CZXTWOJJJ SIX.txt KKKSIXOOO I had a script (2 Replies)
Discussion started by: kmsekhar
2 Replies

8. Shell Programming and Scripting

Split a file with awk

Hi! I have a file like this: a,b,c,12,d,e a,b,c,13,d,e a,b,c,14,d,e a,b,c,15,d,e a,b,c,16,d,e a,b,c,17,d,e I need to split that file in two: If field 4 is equal or higher than 14 that row goes to one file and if it is equal or higher than 15 to another. Can anyone point me in the... (2 Replies)
Discussion started by: Tr0cken
2 Replies

9. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

10. UNIX for Beginners Questions & Answers

Split file using awk

I need to split the incoming source file in to multiple files using awk. Split position is (6,13) : 8 positions All the records that are greater than 20170101 and less than or equal to 20181231 should go in a split file with file name as source... (11 Replies)
Discussion started by: rosebud123
11 Replies
XML_SPLIT(1p)						User Contributed Perl Documentation					     XML_SPLIT(1p)

NAME
xml_split - cut a big XML file into smaller chunks DESCRIPTION
"xml_split" takes a (presumably big) XML file and split it in several smaller files. The memory used is the memory needed for the biggest chunk (ie memory is reused for each new chunk). It can split at a given level in the tree (the default, splits children of the root), or on a condition (using the subset of XPath understood by XML::Twig, so "section" or "/doc/section"). Each generated file is replaced by a processing instruction that will allow "xml_merge" to rebuild the original document. The processing instruction format is "<?merge subdocs=[01] :<filename> ?>" File names are <file>-<nb>.xml, with <file>-00.xml holding the main document. OPTIONS
-l <level> level to cut at: 1 generates a file for each child of the root, 2 for each grand child defaults to 1 -c <condition> generate a file for each element that passes the condition xml_split -c <section> will put each "section" element in its own file (nested sections are handled too) Note that at the moment this option is a lot slower than using "-l" -s <size> generates files of (approximately) <size>. The content of each file is enclosed in a new element ("xml_split::root"), so it's well- formed XML. The size can be given in bytes, Kb, Mb or Gb. -g <nb> groups <nb> elements in a single file. The content of each file is enclosed in a new element ("xml_split::root"), so it's well-formed XML. -b <name> base name for the output, files will be named <base>-<nb><.ext> <nb> is a sequence number, see below "--nb_digits" <ext> is an extension, see below "--extension" defaults to the original file name (if available) or "out" (if input comes from the standard input) -n <nb> number of digits in the sequence number for each file if more digits than <nb> are needed, then they are used: if "--nb_digits 2" is used and 112 files are generated they will be named "<file>-01.xml" to "<file>-112.xml" defaults to 2 -e <ext> extension to use for generated files defaults to the original file extension or ".xml" -i use XInclude elements instead of Processing Instructions to mark where sub files need to be included -v verbose output Note that this option can slow down processing considerably (by an order of magnitude) when generating lots of small documents -V outputs version and exit -h short help -m man (requires pod2text to be in the path) EXAMPLES
xml_split foo.xml # split at level 1 xml_split -l 2 foo.xml # split at level 2 xml_split -c section foo.xml # a file is generated for each section element # nested sections are split properly SEE ALSO
XML::Twig, xml_merge TODO
optimize the code any idea welcome! I have already implemented most of what I thought would improve performances. provide other methods that PIs to keep merge information XInclude is a good candidate (alpha support added in 0.04). using entities, which would seem the natural way to do it, doesn't work, as they make it impossible to have both the main document and the sub docs to be well-formed if the sub docs include sub-sub docs (you can't have entity declarations in an entity) AUTHOR
Michel Rodriguez <mirod@cpan.org> LICENSE
This tool is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.12.4 2010-09-20 XML_SPLIT(1p)
All times are GMT -4. The time now is 09:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy