File splitter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File splitter
# 1  
Old 10-18-2012
File splitter

I have below script which does splitting based on a different criteria. can it be amended to produce required result
Code:
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 source and required target is as below. The splitter word is
I_AM_SEPARATOR and it appears at end where file is to be split.

Code:
Source File : XML_DUMP.txt 
LINE1
LINE2
I_AM_SEPARATOR
LINE11
LINE22
I_AM_SEPARATOR
 
Target Files
XML_DUMP_0001.txt
LINE1
LINE2
 
XML_DUMP_0002.txt
LINE11
LINE22

# 2  
Old 10-18-2012
Quote:
Originally Posted by santosh2k2
My source and required target is as below. The splitter word is
I_AM_SEPARATOR and it appears at end where file is to be split.
Why you don't try yourself ?
I think you have lot of examples for splitting file.Smilie
Just change the logic lit bit.Smilie
# 3  
Old 10-18-2012
I am new to awk.. tried changing the commands to do required but am getting error. the logic on this on is different in a way that the separator appears at end. dont know if different command for that
# 4  
Old 10-18-2012
Quote:
Originally Posted by santosh2k2
I am new to awk.. tried changing the commands to do required but am getting error. the logic on this on is different in a way that the separator appears at end. dont know if different command for that
Okies...

Try this..

Code:
SrcFileName=file
awk '
function namec() {
fn="'"${SrcFileName}_"'" sprintf("%04d",++n) ".txt"
}
NR==1{namec()}
{if($0 ~ /I_AM_SEPARATOR/){namec()}else{print > fn}}' $SrcFileName

Try to understand the logic..Smilie

If Separator present at the start of the line then just remove.. NR==1{namec()}

Code:
SrcFileName=file
awk '
function namec() {
fn="'"${SrcFileName}_"'" sprintf("%04d",++n) ".txt"
}
{if($0 ~ /I_AM_SEPARATOR/){namec()}else{print > fn}}' $SrcFileName


Last edited by pamu; 10-19-2012 at 03:58 AM.. Reason: added info..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

awk or perl script for preposition splitter

Hello, I am writing a Natural Language Parser and one of the tools I need is to separate prepositional phrase markers which begin with a Preposition. I have a long list of such markers (sample given below)and am looking for a script in awk or perl which will allow me to access a look-up file... (2 Replies)
Discussion started by: gimley
2 Replies

3. Shell Programming and Scripting

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. awk... (10 Replies)
Discussion started by: santosh2k2
10 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

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

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

7. Shell Programming and Scripting

Syllable splitter in Perl

Hello, I am a relative newbie and want to split Names in English into syllables. Does anyone know of a perl script which does that. Since my main area is linguistics, I would be happy to add rules to it and post the perl script back for other users. I tried the CPan perl modules but they don't... (6 Replies)
Discussion started by: gimley
6 Replies

8. Programming

Help with splitter code in JAVA

I was creating a file using splitter and printwriter. The result in the file come out as: TO:bbb,ccc,eee Instead of, TO:bbb TO:ccc TO:eee May I know what's wrong with this? (1 Reply)
Discussion started by: eel
1 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
Login or Register to Ask a Question