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
DBAUTOSPLIT(1)						      DocBook Splitting tools						    DBAUTOSPLIT(1)

NAME
dbautosplit - automaticaly split DocBook document SYNOPSIS
dbautosplit [options] input_file [output_template] dbautosplit --help DESCRIPTION
dbautosplit is tool for automaticaly splitting DocBook documents. It use XInclude (see <http://www.w3.org/TR/xinclude/>) for include docu- ment fragments. This allow split large XML file to smaler files withal preserve validation. Splitting is possible in any element. Default is "book", "part", "chapter", "article", "preface", "reference" and "refentry". Morewer you may specify maximum depth level. Output file names are full customizable. It is possible to get tree structure corespond to hiearchy of sections in DocBook document. All "xml:base" attributes (see <http://www.w3.org/TR/xmlbase/>) are processed and them removed. Relative references in "fileref" and "url" attributes are properly rewrited. So it is posible to use dbautosplit (with maximum level depth set to zero) as inteligent "xml:base" attribute remover and so as intelignet DocBook copyier. If no output teplate is specified, default out/index.xml is used. See TEMPALTE EXPANSION below. OPTIONS
--split=ELEMENTS Coma-separated list of XML elements in which is required spliting. Asterisk ('*') means any tag. The default is "book", "part", "chap- ter", "article", "preface" "reference" and "refentry". --split-add=ELEMENTS Coma-separate list of XML elements which are addeded to previously specified tags list or to the default list. See --split otpion. --split-del=ELEMENTS Coma-separated list of XML elements which are removed from previously specified tags list or from the default list. See --split option. -l DEPTH, --level=DEPTH Specify recursion maximum depth level to DEPTH. The default maximum depth is 1. --lo-dirs=PATHS Coma-separated list of paths in which are located local objects (see LOCAL OBJECTS below). All paths are expanded as the standart Unix shell would do, see File::Glob(3perl) for details. Relative paths is related to curent working directory. If path contains $XML_BASE at the begining it is replaced by real base of input document, see option --xmlbase below. Default paths are LocalObjects, MediaOb- jects, ImageObjects and #Pictures in base directory of input document. --lo-dirs-add=PATHS Coma-separeted list of paths which are addeded to preiously specified or the default local objects paths. See --lo-dirs option. --lo-dir=DIRECTORY Name of local objects directory for newly created documents (see LOCAL OBJECTS below). Relative path is related to newly created docu- ments. Default directory is LocalObjects. --lo-move Move local objects instead copy them. See LOCAL OBJECTS below. -o=TEMPLATE, --output-other=TEMPLATE TEMPLATE for other (included) output files (see TEMPALTE EXPANSIONS below). Relative path is related to parent. The default is %attr(id)?:%text(%name()-%02index())/index.xml. --xmlbase=URI XML base URI (Uniform Resource Identifier) for input document. See <http://www.w3.org/TR/xmlbase/> for details. You may need change default if you simple copy an DocBook document without copying referenced files nor changing "fileref" attributes. On this case set XML base to orginal location. Note, URI is required not (unix) file name. --xi-dtd[=FILE] Append minimal internal subset for support "xi:include" element. If FILE is specified then entiry reference to this external subset is inserted instead. Relative path is related to curent work directory. --encoding=ENCODING Enforce encoding for all output files. -v, --verbose Increase verbosity level. -q, --quiet Decrease verbosity level. -V, --version Dump version information and exit. -h, -?, --help Dump help screen and exit. --man Show this manual page and exit. LOCAL OBJECTS
Local objects are special files (pictures, audio files, ...) in directories specified by --lo-dirs option. If DocBook document refer (via "fileref" attribute) to this directory, the files are copied (or moved) to local "Local Objects Directory" specified by --lo-dir option. Moreower all files with same name and differ suffix (after last dot) are copied (or moved) too. TEMPLATE EXPANSIONS
In output file names are recognized special templates with following syntax: TEMPLATE := %[FORMAT]COMMAND(OPTIONS)[?TEMPLATE[:TEMPLATE]] where FORMAT is format modifier as for "%s" format in "printf" command, and COMMAND(OPTIONSB) is one of (case insensible): Attribute(NAME), Attr(NAME) Replace template by attribute value. NodeName(), Name() Replace template by name of node. Depth() Replace template by current depth. Index() Replace template by an unique increasing integer. Text(STRING), Txt(STRING) Replace template by arbritrary string. All templates in STRING are expanded at first. PerlEval(CODE), Eval(CODE) Replace template by result of evalution arbritraty perl code. All templates in CODE are expanded at first. Optionaly the first template can by folloved question mark, second temlate, colon mark and third temlpate. On this case if expansion of the first template is emty the second template is expanded otherwise third template is expanded. EXAMPLES
dbautosplit --level=0 --om=new/mydoc.xml old/mydoc.xml Copy file mydoc.xml from old/ to new/ directory, remove all xml:base attributes and properly rewrite all relative fileref references so they refer to same files. If they are reference to files in local objects directoryes, they will be copyed. dbautosplit --om=new/mydoc.xml --split-add="section" old/mydoc.xml Same as above, but new/mydox.xml is now splited in defaults elements plus in element "section". Name of output files according to default output template (except main output file which is specified) which is %attr(id)?:%text(%name()-%02index())/index.xml. That mean every splited part create own directory in new directory and file index.xml in this directory. Name of this directory will be the value of "id" attribute or name of splited element followed by dash and an integer if there is no "id" attribute specified. dbautosplit --oo='%eval(uc("%attr(id)?:%text(%name()-%index())/index.xml"))' old/mydoc.xml The output files names will have the same name as in default case but in uper case. BUGS AND TODO
o How about cross-document ID/IDREF? Help me. o Magnle entity referenced by entityref and targetdoent attributes. SEE ALSO
dbmerge(3), "DocBook: The Definitive Guide" <http://www.docbook.org/tdg/en/html/docbook.html> AUTHOR
Martin Lazar <mlazar@suse.cz> 0.6 2004-01-27 DBAUTOSPLIT(1)
All times are GMT -4. The time now is 06:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy