Hi..
I am sorry because not produced my full script. This is my mistake and I am very sorry. Below are my full script..

#!/usr/bin/perl -w use strict; use warnings; use POSIX qw(strftime); use constant FILEHDR => 66; use constant DATALEN => 222; use constant FILETRL => 69; use Cwd; use Getopt::Long; my @hdrname = ( "Record Type", "Date", "Record Size", "Number Record", "Application", "Version", "Data Rec. Type", "Extra", "Byte", "newLine" ); my @hdrsize = (3, 14, 4, 8, 16, 5, 3, 8, 4, 2); my $data; my @trlname = ( "Record Type", "Date", "Record Size", "Number Record", "Application", "Version", "Data Rec. Type", "Extra", "newLine" ); my @trlsize = (6, 28, 8, 14, 32, 10, 6, 32, 2); my @dataname = ( "Record Type", "IRVC Key", "Card Number", + "Serial Number", "Issue Code", "Lot Number", "Source Zone", + "Destination Zone", "Caller Number", "Dialed Number", "Convention Ti +me", "Call connect Time", "Call Release Time", "Valued Deducted", "Breakage Valu +e", "MBI", "Carrier", "Carriage Return" ); my @datasize = (4, 24, 32, 32, 32, 16, 32, 32, 40, 40, 24, 28, 28, 18, 18, 10, 32, 2 ); my ($help,$raw,$std,$input,$output,$fname); GetOptions ( "h|help" => \$help, "raw|r" => \$raw, "std|s" => \$std, "input|i=s" => \$input, "output|o=s" => \$output, "filename|f=s" => \$fname, ) or usage(); sub usage { if ($help) { open (HELP, ">/tmp/help.txt"); print (HELP "USAGE: $0 -[hrs] -i <input dir> -o <outpu +t dir> -f <input_file>\n"); print (HELP "Try \"-h\" or \"--help\" for more informa +tion\n\n"); print (HELP "NAME\n"); print (HELP "\t$0 - encode SINGTEL Data Format\n\n"); print (HELP "DESCRIPTION\n"); print (HELP "\t-f or --filename : Assigned input file +name\n"); print (HELP "\t-i or --input : Assigned input direc +tory for the input file\n"); print (HELP "\t-o or --output : Assigned output dire +ctory\n"); print (HELP "\t-r : Display raw record\n +"); print (HELP "\t-s : Display standard rec +ord\n\n"); print (HELP "EXAMPLE\n"); print (HELP "\t>> Display SINGTEL record in standard o +utput\n"); print (HELP "\t\t $0 -s -f UNICA.0352.20091214\n\n"); print (HELP "\t>> Display SINGTEL record including raw + record\n"); print (HELP "\t\t $0 -r -f UNICA.0352.20091214\n\n"); print (HELP "\t>> Convert SINGTEL record into CSV file + format\n"); print (HELP "\t\t $0 -f UNICA.0352.20091214\n\n"); print (HELP "\t>> Convert SINGTEL record into CSV form +at and save into output directory\n"); print (HELP "\t\t $0 -i /input/directory/name/only -f +UNICA.0352.20091214 -o /output/directory/name/only\n\n"); close (HELP); system ("more /tmp/help.txt"); system ("rm -f /tmp/help.txt"); } else { print ("USAGE: $0 -[hrs] -i <input dir> -o <output dir +> -f <input_file>\n"); print ("Try \"-h\" or \"--help\" for more information\ +n\n"); } } # Convert each two-digit hex number back to an ASCII character. sub hextoasc { (my $asc = shift) =~ s/([a-fA-F0-9]{2})/chr(hex $1)/eg; return $asc; } sub print_header { my $data = shift; my $loc = 0; my $nloc = 0; my $filehdr = unpack "A*", $data; my $fileval = unpack "H*", $data; print ("FILE HEADER\n"); for (my $i = 0; $i < @hdrname; $i++) { my $len = $hdrsize[$i]; if ($hdrname[$i] =~ /newLine/) { printf (" %-20s : %-15s >%s<\n", $hdrname[$i +], substr($fileval,$nloc,$len*2), substr($fileval, $nloc, $len*2)); } else { printf (" %-20s : %-15s >%s<\n", $hdrname[$i +], substr($filehdr, $loc, $len), substr($fileval, $nloc, $len*2)); } $loc = $loc + $len; $nloc = $nloc + $len * 2; } } sub print_trailer { my $data = shift; my $loc = 0; my $trlhdr = unpack "A*", $data; my $trlval = unpack "H*", $data; print ("FILE HEADER\n"); for (my $i = 0; $i < @trlname; $i++) { my $len = $trlsize[$i]; if ($trlname[$i] =~ /newLine/) { printf (" %-20s : %-15s >%s<\n", $trlname[$i +], substr($trlval,$loc,$len*2), substr($trlval, $loc, $len)); } else { printf (" %-20s : %-15s >%s<\n", $trlname[$i +], hextoasc(substr($trlhdr, $loc, $len)), substr($trlhdr, $loc, $len) +); } $loc = $loc + $len; } } sub display_data { my $data = shift; my $val; my $tmp; my $cdr; my $loc = 0; print ("\nDATA\n") if ($raw); for (my $i = 0; $i < @dataname; $i++) { my $len = $datasize[$i]; my $fname = $dataname[$i]; if (uc($fname) =~ /RECORD TYPE|CANCEL TYPE/) { $cdr = hextoasc (substr($data, $loc, $len)); $val = substr($data, $loc, $len); } elsif (uc($fname) =~ /RECORD NUMBER/) { $cdr = hextoasc (substr($data, $loc, $len)); $val = substr($data, $loc, $len); } else { $cdr = hextoasc (substr($data, $loc, $len)); $val = substr($data, $loc, $len); } $cdr =~ s/\s+//g; $val =~ s/\s+//g; if ($raw) { printf (" %-20s: %-25s >%s<\n", $fname, $cdr +, $val); } elsif ($std) { printf (" %-20s: %-25s\n", $fname, $cdr); } else { if ($output) { printf (OUTPUT "%s,", $cdr); } else { printf ("%s,", $cdr); } } $loc = $loc + $len; } if ($output) { print (OUTPUT "\n"); } else { print "\n"; } } #################################### MAIN ############################ # $dir = getcwd; if ($fname) { if (!defined ($input)) { $input = getcwd; } if (defined ($output)) { open (OUTPUT, ">$output/$fname.csv"); } open (DATA, "$input/$fname"); binmode DATA; read (DATA, $data, FILEHDR); print_header($data) if ($raw); while (read (DATA, $data, DATALEN) != 0) { my $hex = unpack "H*", $data; my $rtype = hextoasc (substr($hex, 0, 2)); if ($rtype =~ /T/) { print_trailer($hex) if ($raw); } else { display_data($hex); } } close(DATA); close(OUTPUT) if ($output); } else { usage(); }

In reply to Re^2: How to read a binary file with file header, data and file trailer ? by bh_perl
in thread How to read a binary file with file header, data and file trailer ? by bh_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.