Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Friends I am stuck in the file feed problem I have to load the following file onto informix The file I have is:
944|O|1234567890123|14NOV2010|15NOV2010|01JAN3000|C|45678|COMM||045 094|O|1099721190211|14NOV2010|15NOV2010|01JAN3000|C|35077|FREE||044 928|O|1234567890123|14NOV2010|15NOV2010|01JAN3000|C|45678|FREE||030 028|O|2009876543210|14NOV2010|15NOV2010|01JAN3000|C|99212|COMM||054
I need to change the file in the following format where JAN - DES months will be changed to the digit numbers 1-12 and position is Shifted to the format MM\DD\YYYY I am looking for the following structure
944|O|1234567890123|11\14\2010|11\15\2010|01\01\3000|C|45678|COMM||045 094|O|1099721190211|11\14\2010|11\15\2010|01\01\3000|C|35077|FREE||044 928|O|1234567890123|11\14\2010|11\15\2010|01\01\3000|C|45678|FREE||030 028|O|2009876543210|11\14\2010|11\15\2010|01\01\3000|C|99212|COMM||054
Perl/Sed/Awk any thing easy will do. Thanks in Advance. Shan2210.

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: string manipulation
by kennethk (Abbot) on Nov 19, 2010 at 19:51 UTC
    Please read Writeup Formatting Tips. In particular, you have asked for help on parsing a file, but I have little doubt that the act of running it though a browser has mangled it from its original form. Code and data should be wrapped in <code> tags to maintain formatting and whitespace and prevent site-specific mangling. As well, as it says in How do I post a question effectively?, "Don't post real email addresses, usernames, or passwords".

    What have you tried? What didn't work? What is your experience with Perl? We appreciate shown effort around here.

    Examination of your data implies you would like to transform dates from DDMONYEAR to MM\DD\YEAR format. You can do this is a fairly trivial fashion using a hash on month names and a regular expression:

    #!/usr/bin/perl use strict; use warnings; my $string = '944|O|1234567890123|14NOV2010|15NOV2010|01JAN3000|C|4567 +8|COMM||045 094|O|1099721190211|14NOV2010|15NOV2010|01JAN3000|C|35077 +|FREE||044 928|O|1234567890123|14NOV2010|15NOV2010|01JAN3000|C|45678| +FREE||030 028|O|2009876543210|14NOV2010|15NOV2010|01JAN3000|C|99212|C +OMM||054'; my %months = (JAN => '01', FEB => '02', MAR => '03', APR => '04', MAY => '05', JUN => '06', JUL => '07', AUG => '08', SEP => '09', OCT => '10', NOV => '11', DEC => '12', ); $string =~ s/ (\d{2}) # Two digits ([A-Z]{3}) # Three capital letters (\d{4}) # Four digits /$months{$2}\\$1\\$3/xg; print $string;

    Note this is fragile, as are most regular expressions.