Fixed Width files to CSV using AnyData


File conversion is one of those things that immediately brings perl to mind. I recently had a need to quickly come up with something that converted fixed width files output by sql server to CSV files - for use by the Remedy Import Tool.

I took a quick look around and found DBD::RAM, which has apparently grown up into DBD::AnyData.

After running through the install process - which is extremely simple using MCPAN, it took me about 6 minutes to read through the documentation and write the 4 line perl script to get the job done. I'm not bragging, just trying to point out how simple it is.

The installation of the module is simple:

First start the MCPAN shell:

perl -MCPAN -e shell

and then install the module:

install DBD::AnyData

For this example, I'm going to start with a simple fixed width text file with name, rank, and serial number. The name column is 10 characters wide, rank is 5, and serial number is 4. Let's name this file fixed.txt.

BOB JONESSGT  A123
KALLESTADMAJORE982

The perl routine to convert this to CSV is as follows:

collapsehide line numbers
Sample Code
 1<br />
 2#!/usr/bin/perl<br />
 3use DBI;<br />
 4my $dbh = DBI->connect("dbi:AnyData(RaiseError=>1):");<br />
 5$dbh->func('Fixed','fixed.txt','CSV','converted.csv',{pattern => 'A10 A5 A4'},'ad_convert');<br />
Code ©SteveKallestad.com

It outputs the data to "converted.csv". The pattern hash is simply a listing of the column widths - A10 A5 A4 means - a 10 character column, a 5 character column, and a 4 character column. I did notice while testing this that if a column contains a comma character, it is automatically surrounded by quotes in the CSV.



Fixed Width files to CSV using AnyData Commentary