Need a file conversion utility to fixed record length files

Posted on 2004-11-10
Last Modified: 2010-04-17
I need a small console utility, which can do the following:

Read a flat file where each record is of variable length, terminated with CR/LF.
Produce a flat file, fixed (adjustable) record length, blank-padded records and terminated with CR/LF.

Example ('.' symbolises a space/blank here):

This is the dataCR/LF
Here are more data in the fileCR/LF
Last recordCR/LF

Outputfile (a record width of 40 bytes chosen):
This is the data......................CR/LF
Here are more data in the file........CR/LF
Last record...........................CR/LF

Anything that will work on 32 bit Windows AND handle long filenames will do.

I know it's out there somewhere - but I just can't locate it...


Question by:KDK
    LVL 3

    Accepted Solution

    I don't think there is such a specialized command-line utility, you will probably need to create a small program or a script to perform the required task. If you have Windows Script Host installed, a simple script can be created.

    If you don't need to process multiple files in a batch, you can also use MS Excel to do exactly what you need. If so, let me know and I'll send you a brief tutorial.

    Author Comment

    Hi PodExpert,

    No Excel won't do it - need to be run 'in batch'.

    I do'nt think my requst is *that* special - and spending a few hours on the net, actualy led me to a Perl script yielding exactly what I needed. It's included below.

    Case closed - thanks for your input.


    #!C:\Perl\bin -w
    #  pad data records with spaces to 126 chars plus '\n'.
    #  Usage:
    #      the output will be named "$INPUTFILE.out"
    #  Note: Ignore the trailing newline character.  If data
    #        has 126 chars in it, enter 126 chars on the command
    #        line.  Output will be 126 chars plus '\n'.
    #        Update:  That's true only when inputting regular text
    #                 files.  "bill_print" (?) contains things like:
    #                 "^A^T"; that's four chars ^ + A + ..., not to
    #                 mention a lot of \210 -ish things.
    #                 wc -c on that reveals 400 odd chars.  vi and emacs
    #                 see the '^X' as two separate chars.  "more" and
    #                 wc -c see the ctrl-qN pair as the _real control char_!
    #                 "more" finds an EOF after four lines and quits.
    #                 head -1 blah | od -c displays ascii lines
    #                 terminated by \n with lots of embedded "0"'s.
    #  12Oct2001   s. keeling       0001     start.

    use strict;

    my ( $line, $input, $output, $start, $rec, $numchars, $list );

    # $input = "/home/ecperdi/skeeling/data/P0510DCIPC01OUT_10.dat";
    die qq(usage: $0 NUMCHARS INFILE [list]\n)
        if @ARGV < 2;

    $numchars = "$ARGV[0]";
    $input = "$ARGV[1]";
    $list = "$ARGV[2]";

    if( defined( $input ) ) {

        chomp( $input );

        $output = "$input" . ".out";

        open( INFILE, qq(< $input) )
            or die qq( $0: cannot open $input: $!\n );

        open( OUTFILE, qq(> $output) )
            or die qq( $0: cannot open $output: $!\n );

        while ( $line = <INFILE> ) {

            chomp( $line );

            if ( $line =~ /^$/ ) {
                print OUTFILE $line . "\n";

            if( defined( $line ) ) {


                $start = length( $line );
                if ( defined( $list ) ) {
    #                print "$rec: $start chars long\n";

                while ( $start < $numchars ) {
                    $line .= " ";

                print OUTFILE $line . "\n";
    LVL 3

    Expert Comment

    as I said, it could be done via a script. But for such a simple task, it is usually easier to write the script instead of speding a few hours finding it - of course, you need to know, how to do it. Or ask an expert :)


    Author Comment


    You're completely right, and that's what you said in the first place.

    Yeah - writing a script like this is easy - just need to be on the right platform and with the right tools available...

    Once again: Thanks for your replies, this really is a great forum!


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now