MinhTech.com

Yet another technology tutorial blog.

Perl Script to Convert or Parse EBCDIC File

By • Mar 16th, 2011 • Category: Perl

Here is a simple Perl script to convert/parse data in IBM Extended Binary Coded Decimal Interchange Code, EBCDIC, to ASCII format.

Analyze EBCDIC File:

Suppose we have a file in EBCDIC format with the following field name, offset, size, and data type elements:

field_01 0 7 packed decimal(14)
field_02 7 4 integer(4)
field_03 11 4 ebcdic string(4)

Note the numeric field length. Each byte of a packed decimal contains two digits. So “field1″ is 7 * 2 = 14 = 13 digits plus the symbol.

Create the Perl Script:

> vi ebcdic2ascii.pl
#!/usr/bin/perl -w
use strict;
use Convert::EBCDIC;
sub convert2ascii_decimal {
   my ($number) = unpack('H*', $_[0]);
   my ($sign) = chop $number;
   my ($scale) = $_[1];
   if ($scale > 0) {
      $number = (substr $number, 0, -$scale) . '.' . (substr $number, -$scale);
   }
   if ($sign eq 'd') {
      $number = -$number;
   }
   return $number;
}
sub convert2ascii_integer {
   my ($integer) = @_;
   return unpack('i', $integer);
}
sub convert2ascii_string {
   my ($string) = @_;
   return Convert::EBCDIC::ebcdic2ascii($string);
}
sub filewrite {
   my ($file, $string) = @_;
   open FH, '>>', $file or die $!;
   select FH;
   print "$string\n";
   select STDOUT;
   close FH or die $!;
}
sub recordparse {
   my ($record) = @_;
   my $string;
   $string = undefined2defined(convert2ascii_decimal((substr $record, 0, 7), 2));
   $string = $string . chr(9) . undefined2defined(convert2ascii_integer((substr $record, 7, 4)));
   $string = $string . chr(9) . undefined2defined(convert2ascii_string((substr $record, 11, 4)));
   return $string;
}
sub undefined2defined {
   my ($string) = @_;
   if (defined $string) {
      return $string;
   }
   else {
      return '';
   }
}
my $string;
open (FILE, $ARGV[0]) or die $!;
while ((read FILE, $string, 14) != 0) {
   filewrite ($ARGV[1], recordparse($string));
}
close(FILE) or die $!;
exit;

In this example, the main procedure is reading in 14 characters at a time from the file, which is the record length. The function recordparse parses each record by calling convert2ascii_decimal, convert2ascii_integer, and convert2ascii_string; these functions unpack packed decimals, unpack integers, and convert EBCDIC strings to ASCII respectively.

Note that the code using convert2ascii_decimal has a third highlighted parameter, field scale, in addition to field offset and field length. In this example, “field1” has a field offset of 0, a field length of 7, and a field scale of 2 (or 2 decimal places).

Finally, Run the Script:

> perl ebcdic2ascii.pl ebcdic.ods ascii.txt

is a technology junkie.
Email this author | All posts by

One Response »

  1. Hi,

    When i run this code, I am getting an error message –

    Undefined subroutine Convert::EBCDIC::ebcdic2ascii ……

    Could you please help.

    Thanks,
    Arijit

Leave a Reply