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

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

Why you don't try yourself ?
I think you have lot of examples for splitting file.:slight_smile:
Just change the logic lit bit.:smiley:

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

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..:slight_smile:

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

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