Learn how to a build a cloud-first strategyRegister Now

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

Perl regex substitution

Quick question:

What am I doing wrong?  The script doesn't print anything.

#!/usr/bin/perl

use warnings;
use strict;

my %mon = ('Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06',
           'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12');

sub MmmDd
{
    my $mmmddyyyy = shift;
    my ($mmm, $dd, $yyyy) = split(/\s+/, $mmmddyyyy);
    my $string =  "$yyyy-$mon{$mmm}-" . sprintf("%02d", $dd);
    return "$string";
}


while(<>)
{
    s/([A-Z][a-z][a-z])\s+(\d{1,2})\s+(\d{4})/MmmDd("$1 $2 $3")/eg;
}

Open in new window


Sample data:
81      223456789       Jan 2 1999 02:30 am     1222222232224222        123232345
84      111334411       Jan 7 1999      7777 7777 7777  123232345
85      111334444       Jan 2 1999      7777 7777 7777 7777     123232345
86      777777779       Jan 2 1999      7777 7777 6663  111223333
87      111333333       Jan 2 1999      00001111222233334444    111223333
89      111222222       Jan 2 1999      1234333331114123        923457789
90      111444444       Jan 2 1999      111112111121111 923457789
91      111223333       Jan 2 1999      1111222233334444        923457789
92      777777778       Jan 2 1999      777766661234X   222222221
93      111334444       Jan 05 1999 02:30 pm    X7777 7777      222222221
94      777777778       Jan 2 1999      777766661234X   222222221
95      923456789       Jan 3 1999      3131 7777 7777 7777     443232366
99      776677778       Jan 2 1999      1222222232224222        923457789

Open in new window


There may be  more than one dates.
I tried (?e) (?-e) but that did not work either.
0
farzanj
Asked:
farzanj
2 Solutions
 
jhheiderCommented:
Add this before line 21:

print;

Done. :)
0
 
lwadwellCommented:
Try adding a
    print;
or am I missing something?
0
 
farzanjAuthor Commented:
No, you are right :) Some days are just bad.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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