Happy New Year !
Try and adapt the new version of the script (chowhan.ksh):
#!/usr/bin/ksh
InFile=./chowhan.dat # Input file
OutSpec='./chowhan_%d.dat' # Outfiles specification, %d for file number
P1max=5 # Split every Pmax P1 record
rm -f ./chowhan_*.dat* >/dev/null 2<&1
/usr/xpg4/bin/awk -v P1MAX="${P1max}" -v OUT="${OutSpec}" '
#----------------------
# Functions
#-----------------------
function closeOutFile() {
if (FileIndx > 0) close(FileName);
}
function openNewOutFile() {
FileName = sprintf(OUT ".tmp", ++FileIndx);
Files[FileIndx ] = FileName;
Files[FileIndx, "1Z"] = 0;
Files[FileIndx, "P1"] = 0;
SubHeaderCount = 0;
P1Zcount = 0;
P1count = 0;
}
function switchOutFile() {
closeOutFile();
openNewOutFile();
}
function writeSubHeader() {
if (! FileIndx) openNewOutFile();
Files[FileIndx, "1Z"]++;
printf "1Z%0.2d%s\n", ++SubHeaderCount, SubHeader > FileName;
}
function writeP1() {
if (P1count >= P1MAX) {
switchOutFile();
writeSubHeader();
}
P1Zcount++;
P1count++;
Files[FileIndx, "P1"]++;
printf "P1%0.5d%s\n", P1Zcount, substr($0,8) > FileName;
}
#----------------------
# Actions
#----------------------
/^1X/ {
Header = $0;
HeaderHead = substr($0, 1, length($0)-15);
HeaderMid = substr($0, length($0)-13, 1);
HeaderTail = substr($0, length($0)-2);
FileIndx = 0;
next;
}
/^1Z/ {
SubHeader = substr($0, 5);
P1Zcount = 0;
writeSubHeader();
next;
}
/^P1/ {
writeP1();
next;
}
/^4C/ {
Trailer = $0;
exit;
}
{
print $0 > FileName;
next;
}
END {
closeOutFile();
HeaderId = "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (i=1; i<=FileIndx; i++) {
FileName = Files;
print Trailer >> FileName;
close(FileName);
ResultFileName = sprintf(OUT, i)
printf("%s%1.1s%s%0.5d%0.5d%s\n", HeaderHead,
substr(HeaderId, i, 1) ,
HeaderMid,
Files[i,"1Z"],
Files[i,"P1"],
HeaderTail ) > ResultFileName;
close(ResultFileName);
system(sprintf("/usr/bin/cat %s >>%s && /usr/bin/rm %s", FileName, ResultFileName, FileName));
}
}
' ${InFile}
Input file (chowhan.dat) :
1X.....................1234567890123456111T0000300013XT1
1Z01............
P100001............
Q1........
R1.................
P100002.........
Q1..................
R1......................
1Z02............
P100001..........
Q1.............
R1...........
S1.....
P100002..........
Q1.............
R1...........
S1.....
P100003..........
Q1.............
R1...........
S1.....
P100004..........
Q1.............
R1...........
S1.....
P100005..........
Q1.............
R1...........
S1.....
1Z03............
P100001..........
Q1.............
R1...........
S1.....
P100002..........
Q1.............
R1...........
S1.....
P100003..........
Q1.............
R1...........
S1.....
P100004..........
Q1.............
R1...........
S1.....
P100005..........
Q1.............
R1...........
S1.....
P100006..........
Q1.............
R1...........
S1.....
4C.........
Running the script :
$ ls chowhan*
chowhan.dat chowhan.ksh
$ ./chowhan.ksh
$ ls chowhan*
chowhan_1.dat chowhan_2.dat chowhan_3.dat chowhan.dat chowhan.ksh
Output file 1 (chowhan_1.dat) :
1X.....................1234567890123456110T0000200005XT1
1Z01............
P100001............
Q1........
R1.................
P100002.........
Q1..................
R1......................
1Z02............
P100001..........
Q1.............
R1...........
S1.....
P100002..........
Q1.............
R1...........
S1.....
P100003..........
Q1.............
R1...........
S1.....
4C.........
Output file 2 (chowhan_2.dat) :
1X.....................1234567890123456111T0000200005XT1
1Z01............
P100001..........
Q1.............
R1...........
S1.....
P100002..........
Q1.............
R1...........
S1.....
1Z02............
P100001..........
Q1.............
R1...........
S1.....
P100002..........
Q1.............
R1...........
S1.....
P100003..........
Q1.............
R1...........
S1.....
4C.........
Output file 3 (chowhan_3.dat) :
1X.....................1234567890123456112T0000100003XT1
1Z01............
P100001..........
Q1.............
R1...........
S1.....
P100002..........
Q1.............
R1...........
S1.....
P100003..........
Q1.............
R1...........
S1.....
4C.........