[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Need a file conversion utility to fixed record length files

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):

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

TIA!

/Hans
0
KDK
Asked:
KDK
  • 2
  • 2
1 Solution
 
PodExpertCommented:
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.
0
 
KDKAuthor Commented:
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.

/Hans

#!C:\Perl\bin -w
#
#  pad data records with spaces to 126 chars plus '\n'.
#
#  Usage:
#
#      pad_002.pl NUMCHARS INPUTFILE
#
#      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 =~ /^$/ ) {
            $rec++;
            print OUTFILE $line . "\n";
            next;
        }

        if( defined( $line ) ) {

            $rec++;

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

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

            print OUTFILE $line . "\n";
        }
    }
}
0
 
PodExpertCommented:
Hi,
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 :)

Regards.
P.
0
 
KDKAuthor Commented:
PodExpert,

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!

/Hans
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now