Break one long string into multiple fixed length lines

This is actually a KSH under Unix System Services (Z/OS), but hoping I can get a standard AIX/KSH solution to work...

I have a very large, single line file in Windows, that we download via FTP, with the "SITE WRAP" option, into a Z/OS file with an LRECL of 200. This essentially breaks the single long line down into multiple 200 byte records.

We are converting from FTP, to (OPENSSH) SFTP, which unfortunately does not include the "SITE WRAP" ability. Therefore, I'm attempting to come up with another means of accomplishing the task.

I have downloaded the file to USS, and have written a simple script to read in the record and split it out into multiple, 200 byte records, and write those out to a new file...

#!/bin/ksh                             
echo 'Starting break..'                
rm -f /homesh/ljamoo/SEQMAST2          
CPOS=1                                 
echo 'Loop..'                          
while read TLINE                       
 do                                    
 NLINE=`expr substr $TLINE $CPOS 200`  
 echo $NLINE >> /homesh/ljamoo/SEQMAST2
 CPOS=`expr $CPOS + 200`               
 done </homesh/ljamoo/SEQMAST          
echo 'Done.'                           
exit              

However, this is not working as USS is apparently unable to read in the entire record into the TLINE variable...

Starting break..                                                                
Loop..                                                                          
read: /homesh/ljamoo/scripts/break.sh 6: FSUM9225 no memory: EDC5132I Not enough memory.
/homesh/ljamoo/scripts/break.sh 11: FSUM9225 no memory: EDC5132I Not enough memory.
/homesh/ljamoo/scripts/break.sh 11: FSUM9225 no memory: EDC5132I Not enough memory.
/homesh/ljamoo/scripts/break.sh 11: FSUM9225 no memory: EDC5132I Not enough memory.
....

So, does anyone have any suggestions on how else to break it down? Being a mainframe dinosaur, I have yet to wrap my head around the "sed" and "awk" commands, but hoping maybe there is something like that, which would accomplish this.

Thanks!

man fold

1 Like

Since 'fold' also depends upon standard input, doesn't that limit the line length to the system default max line length.
An alternative solution would be to write a cobol program using GNUcobol, making the input file sequential with fixed record length (ie no record separator), and the output file line sequential.

Worked like a charm! Thanks!

If I correctly understand what you're trying to do (and since you aren't quoting the expansion of $NLINE in your echo statement, I'm not sure that I do), you might find that using dd is a lot easier than writing a COBOL program:

dd if=/homesh/ljamoo/SEQMAST of=/homesh/ljamoo/SEQMAST2 bs=200 conv=unblock

This will strip off trailing space characters from your input file (like your echo does), but it won't strip leading whitespace and won't convert sequences of spaces and tabs in the middle of your input lines to a single space (like your echo does).