Solved

Regular Expressions Perl

Posted on 2010-08-19
4
381 Views
Last Modified: 2012-08-14


Hi,

I am trying to parse a date field with format

dd/mm/YYYY HH:MM:SS

sometimes the date has only one digit and sometime the HH has only
one digit
example of input

7/03/2001 9:09:18,034449599,BGC,WCOM,C3400

I want to look at each field of the date if there is only digit I
concatenate a 0 in front of it, example if the day is 7 I put 07,
if the hour is 9 I put 09. I also want to elimnate the space.

so for the input above the desired output

is
07/03/200109:09:18,034449599,BGC,WCOM,C3400

:

I am able to isolate the dd and remove the space but when I am unable
to isolate the mm and the HH

example:

#!/usr/bin/perl

#use strict;

use warnings;
my $inputFile;
my $inputname = "Sample1Date.txt";
my $outputfile;
my $outputname = "Sample1Date_output.txt";
my $line;
my @lineParts;
my @linePartsDay;
my @linePartsMonth;
my $temp2="";

open (inputFile, "$inputname");
#open ($inputFile, "<$inputname") || die "Can not open input file";
open ($outputfile, ">$outputname") || die "Can not open output file";
my $temp = "";

while ($line = <inputFile>)

{

       @lineParts = split '\,', $line;
       my $date = $lineParts[0];
       @linePartsDay = split '\/', $date;
       my $day = $linePartsDay[0];
       print "day : $day\n";

       #@linePartsMonth = split '\^(\d{2}/\d{2}/)', $date;
       @linePartsMonth = split '\^(//)', $date;

       my $month = $linePartsMonth[0];
       print "month : $month\n";


       #$temp2 = $date;
       #$temp2 =~ s/(\s)+//;

       #$date = $temp2;   # remove space(s) in the middle of date
       print "date : $date\n";
       my $number1 = $lineParts[1];
       my $donor = $lineParts[2];
       my $recipient = $lineParts[3];
       my $routing = $lineParts[4];
       $temp = $date.",".$number1.",".$donor.",".$recipient.",".$routing;
       print $outputfile  "$temp";


}

#close $inputFile;

close $outputfile;

I get:

day: 7  (OK) I can then look at the length of day if it is only 1 digit concatenate a 0 in front of it.
month: date: 7/03/20019:09:18
date: 7/03/20019:09:18

I do not know how to isolate the mm, once I get the month I would like to isolate
the yyyy the HH the MM and the SS and to check each field (once I have isolate I
know how to concate).

I tried various regular expressions for isolating the month but unable...

       #@linePartsMonth = split '\^(\d{2}/\d{2}/)', $date;
       @linePartsMonth = split '\^(//)', $date;

       my $month = $linePartsMonth[0];
       print "month : $month\n";

Do you know how to the isolation for MM, YYYY HH MM SS


0
Comment
Question by:Johannne1
  • 2
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 33481974
$_= '7/03/2001 9:09:18,034449599,BGC,WCOM,C3400';
s/ ?\b(\d)\b/0$1/g;
print;
0
 

Author Comment

by:Johannne1
ID: 33481987
Thanks ozo, I will try this.
0
 
LVL 84

Expert Comment

by:ozo
ID: 33482002
($DD,$MM,$YYYY,$HH,$MM,$SS,$parts)=split/\D+/,'7/03/2001 9:09:18,034449599,BGC,WCOM,C3400',7;
printf "%02d/%02d/%04d%02d:%02d:%02d,%s",$DD,$MM,$YYYY,$HH,$MM,$SS,$parts;
0
 

Author Comment

by:Johannne1
ID: 33482047
Beautiful! 1 line!! you are a regular expression master!
I did:
$_= $line;
s/ ?\b(\d)\b/0$1/g;
print"date : $_\n";

and tried different data and the 0 when 1 digit. I am not sure about the first s . s/
does thhis mean string and then is there a backslash followed by a b what does the b stand for
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now