fixblock.pl -- Convert Stream File to Fixed Block File


This is a Perl script to convert a "stream" data file, in which all data is on one long line with no end of line delimeters, into a fixed block data file with a constant logical record length (LRECL).* This started as a one or two line perl hack with hard-coded parameters, but after giving it to several coworkers, I found that it required more hand-holding (like commandline parameters, usage messages, error messages, etc.). Hence, it's quite a bit longer now, but the gist of it is still one line of code.

This small program has been useful for fixing files incorrectly read from tape and some data files that have been received in stream format via FTP. The program leaves the original data file in its original format. NOTE that the script will fail to produce useful output if, for example, trailing blanks on short data lines were stripped before the input data file was put into stream format. Each data line or record must be LRECL length even when in stream format, in other words.

 

Usage

Assuming this program is executable its commandline is:

fixblock.pl -l# infile outfile

where:

As an example of usage, the command fixblock.pl -l5 test.dat test.out with an input file, test.dat (one line with no line-end character) consisting of:

12345123451234512345123451234512345
will produce this output in test.out:
12345
12345
12345
12345
12345
12345
12345
The user will see the following output on the screen:
(pts/1):~> fixblock.pl -l5 test.dat test.out

fixblock.pl: Done!
7 records processed from 'test.dat'

Output is in 'test.out'

* Yes, there are other ways to do this sort of conversion--such as dd, but most users have trouble with that one. The GNU program task, but it does not end the last line of the file with a newline character. The /usr/bin/fold program from Sun also seems to have this characteristic. I needed a program that would add a newline even on the last line of the program. There are instances, however, when the 'fold' behavior is useful, also.


Back to Kent's Perl Page
Last Modified: Tue Jul 15 16:26:44 EDT 2025