Extracting Date using Regular Expressions

K

Kimi

Hi I am new to perl,

I need some help in regular expressions....

I have a string "Tue Nov 7 14:04:16 2006: Unable to open prev pos
file" in a variable "myvariable" and I need to extract "Tue Nov 7
14:14:16 2006" to another variable..

I also would like to know if there are any way to compare the above
result with Current date and find the difference of days/months

Appreciate your help,
Kimi
 
J

John W. Krahn

Kimi said:
Hi I am new to perl,

I need some help in regular expressions....

I have a string "Tue Nov 7 14:04:16 2006: Unable to open prev pos
file" in a variable "myvariable" and I need to extract "Tue Nov 7
14:14:16 2006" to another variable..

I also would like to know if there are any way to compare the above
result with Current date and find the difference of days/months


$ perl -le'
use POSIX q/mktime/;

my $myvariable = "Tue Nov 7 14:04:16 2006: Unable to open prev pos file";

my $week_day = qr/Mon|Tue|Wed|Thu|Fri|Sat|Sun/;
my $mon_name = qr/Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/;

my ( $mon, $day, $hour, $min, $sec, $year ) = $myvariable =~ /$week_day \s+
($mon_name) \s+ (\d+) \s+ (\d+) : (\d+) : (\d+) \s+ (\d+)/x;

print "$sec, $min, $hour, $day, $mon, $year";

my $old_date = mktime $sec, $min, $hour, $day, ( index( $mon_name, $mon ) -
index( $mon_name, q/Jan/ ) ) / 4, $year - 1900;

my $current_date = time;

my ( $diff_days, $diff_mons ) = ( gmtime $current_date - $old_date )[ 3, 4 ];

print for scalar localtime $old_date, scalar localtime $current_date, "Days:
$diff_days, Months: $diff_mons";
'
16, 04, 14, 7, Nov, 2006
Tue Nov 7 14:04:16 2006
Wed Nov 22 05:48:51 2006
Days: 15, Months: 0




John
 
K

Kimi

John said:
Kimi said:
Hi I am new to perl,

I need some help in regular expressions....

I have a string "Tue Nov 7 14:04:16 2006: Unable to open prev pos
file" in a variable "myvariable" and I need to extract "Tue Nov 7
14:14:16 2006" to another variable..

I also would like to know if there are any way to compare the above
result with Current date and find the difference of days/months


$ perl -le'
use POSIX q/mktime/;

my $myvariable = "Tue Nov 7 14:04:16 2006: Unable to open prev pos file";

my $week_day = qr/Mon|Tue|Wed|Thu|Fri|Sat|Sun/;
my $mon_name = qr/Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/;

my ( $mon, $day, $hour, $min, $sec, $year ) = $myvariable =~ /$week_day \s+
($mon_name) \s+ (\d+) \s+ (\d+) : (\d+) : (\d+) \s+ (\d+)/x;

print "$sec, $min, $hour, $day, $mon, $year";

my $old_date = mktime $sec, $min, $hour, $day, ( index( $mon_name, $mon ) -
index( $mon_name, q/Jan/ ) ) / 4, $year - 1900;

my $current_date = time;

my ( $diff_days, $diff_mons ) = ( gmtime $current_date - $old_date )[ 3, 4 ];

print for scalar localtime $old_date, scalar localtime $current_date, "Days:
$diff_days, Months: $diff_mons";
'
16, 04, 14, 7, Nov, 2006
Tue Nov 7 14:04:16 2006
Wed Nov 22 05:48:51 2006
Days: 15, Months: 0




John

Thanks John....

Regards,
Kimi
 
P

Paul Lalli

John said:
Kimi said:
I have a string "Tue Nov 7 14:04:16 2006: Unable to open prev pos
file" in a variable "myvariable" and I need to extract "Tue Nov 7
14:14:16 2006" to another variable..

I also would like to know if there are any way to compare the above
result with Current date and find the difference of days/months


$ perl -le'
use POSIX q/mktime/;

my $myvariable = "Tue Nov 7 14:04:16 2006: Unable to open prev pos file";

my $week_day = qr/Mon|Tue|Wed|Thu|Fri|Sat|Sun/;
my $mon_name = qr/Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/;

my ( $mon, $day, $hour, $min, $sec, $year ) = $myvariable =~ /$week_day \s+
($mon_name) \s+ (\d+) \s+ (\d+) : (\d+) : (\d+) \s+ (\d+)/x;

print "$sec, $min, $hour, $day, $mon, $year";

my $old_date = mktime $sec, $min, $hour, $day, ( index( $mon_name, $mon ) -
index( $mon_name, q/Jan/ ) ) / 4, $year - 1900;

my $current_date = time;

my ( $diff_days, $diff_mons ) = ( gmtime $current_date - $old_date )[ 3, 4 ];

print for scalar localtime $old_date, scalar localtime $current_date, "Days:
$diff_days, Months: $diff_mons";
'
16, 04, 14, 7, Nov, 2006
Tue Nov 7 14:04:16 2006
Wed Nov 22 05:48:51 2006
Days: 15, Months: 0

Or, if you didn't feel like doing all that work yourself, take
advantage of modules on CPAN, like Date::Manip...

#!/usr/bin/perl
use strict;
use warnings;
use Date::Manip;

my $var = "Tue Nov 7 14:04:16 2006: Unable to open prev pos file";
my ($dttm) = ($var =~ /^(.*\d{4}):/);
my $date = ParseDate($dttm);
die "Couldn't parse date!\n" unless $date;
my $today = ParseDate("today");
my ($Y, $M, $W, $D, $h, $m, $s) =
(DateCalc($date, $today, \my $err, 1) =~ /\d+/g
);
print "Date was $Y year(s), $M month(s), $W week(s), $D day(s), $h
hour(s), $m minute(s) and $s second(s) ago\n";
__END__
Date was 0 year(s), 0 month(s), 2 week(s), 1 day(s), 0 hour(s), 47
minute(s) and 0 second(s) ago


Paul Lalli
 
K

Kimi

Paul said:
John said:
Kimi said:
I have a string "Tue Nov 7 14:04:16 2006: Unable to open prev pos
file" in a variable "myvariable" and I need to extract "Tue Nov 7
14:14:16 2006" to another variable..

I also would like to know if there are any way to compare the above
result with Current date and find the difference of days/months


$ perl -le'
use POSIX q/mktime/;

my $myvariable = "Tue Nov 7 14:04:16 2006: Unable to open prev pos file";

my $week_day = qr/Mon|Tue|Wed|Thu|Fri|Sat|Sun/;
my $mon_name = qr/Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/;

my ( $mon, $day, $hour, $min, $sec, $year ) = $myvariable =~ /$week_day \s+
($mon_name) \s+ (\d+) \s+ (\d+) : (\d+) : (\d+) \s+ (\d+)/x;

print "$sec, $min, $hour, $day, $mon, $year";

my $old_date = mktime $sec, $min, $hour, $day, ( index( $mon_name, $mon ) -
index( $mon_name, q/Jan/ ) ) / 4, $year - 1900;

my $current_date = time;

my ( $diff_days, $diff_mons ) = ( gmtime $current_date - $old_date )[ 3, 4 ];

print for scalar localtime $old_date, scalar localtime $current_date, "Days:
$diff_days, Months: $diff_mons";
'
16, 04, 14, 7, Nov, 2006
Tue Nov 7 14:04:16 2006
Wed Nov 22 05:48:51 2006
Days: 15, Months: 0

Or, if you didn't feel like doing all that work yourself, take
advantage of modules on CPAN, like Date::Manip...

#!/usr/bin/perl
use strict;
use warnings;
use Date::Manip;

my $var = "Tue Nov 7 14:04:16 2006: Unable to open prev pos file";
my ($dttm) = ($var =~ /^(.*\d{4}):/);
my $date = ParseDate($dttm);
die "Couldn't parse date!\n" unless $date;
my $today = ParseDate("today");
my ($Y, $M, $W, $D, $h, $m, $s) =
(DateCalc($date, $today, \my $err, 1) =~ /\d+/g
);
print "Date was $Y year(s), $M month(s), $W week(s), $D day(s), $h
hour(s), $m minute(s) and $s second(s) ago\n";
__END__
Date was 0 year(s), 0 month(s), 2 week(s), 1 day(s), 0 hour(s), 47
minute(s) and 0 second(s) ago


Paul Lalli


Thanks Paul for your help, I tried running the script in my PC, but it
says "Can't locate Date/Manip.pm in @INC (@INC contains:
/System/Library/Perl/5.8.6/darwin-thread-multi-2level
/System/Library/Perl/5.8.6
/Library/Perl/5.8.6/darwin-thread-multi-2level /Library/Perl/5.8.6
/Library/Perl /Network/Library/Perl/5.8.6/darwin-thread-multi-2level
/Network/Library/Perl/5.8.6 /Network/Library/Perl
/System/Library/Perl/Extras/5.8.6/darwin-thread-multi-2level
/System/Library/Perl/Extras/5.8.6 /Library/Perl/5.8.1 .) at datefind
line 4.
BEGIN failed--compilation aborted at datefind line 4."

So the system couldn't find manip.pm. Currently i am using 5.8.6 in mac
OS X. Would appreciate your help if I can get to know what could be the
solution for it...

Thanks in advance,
Kimi
 
J

John Bokma

Thanks Paul for your help, I tried running the script in my PC, but it
says "Can't locate Date/Manip.pm in @INC (@INC contains:

Install it via CPAN.
So the system couldn't find manip.pm.

No, it couldn't find Date/Manip.pm

Also notice that your OS is CaSe SeNSitIVe.
 
B

Ben Morrow

Quoth "Kimi said:
Thanks Paul for your help, I tried running the script in my PC, but it
says "Can't locate Date/Manip.pm in @INC (@INC contains:
So the system couldn't find manip.pm.

Manip.pm: case matters. In fact, Date/Manip.pm would be better, as the
error message said.
Currently i am using 5.8.6 in mac
OS X. Would appreciate your help if I can get to know what could be the
solution for it...

You need to install the module from CPAN.

perldoc CPAN

perldoc ppm if you're using ActivePerl (I'm not sure: you said 'PC', by
which people usually mean 'Windows'; but you also said OSX...).

If you installed Perl through a package manager that may have a way of
installing Perl modules as well, that may be easier to use (or not :) ).

Ben
 
P

Paul Lalli

Thanks Paul for your help, I tried running the script in my PC, but it
says "Can't locate Date/Manip.pm in @INC (@INC contains:

Like I said, it's a module found on CPAN, not included in the core
distrobution. If you don't know what CPAN is, please read:
perldoc -q cpan
perldoc perlmodinstall

Paul Lallli
 
B

boyd

Thanks Paul for your help, I tried running the script in my PC, but it
says "Can't locate Date/Manip.pm in @INC (@INC contains:
/System/Library/Perl/5.8.6/darwin-thread-multi-2level
/System/Library/Perl/5.8.6
/Library/Perl/5.8.6/darwin-thread-multi-2level /Library/Perl/5.8.6
/Library/Perl /Network/Library/Perl/5.8.6/darwin-thread-multi-2level
/Network/Library/Perl/5.8.6 /Network/Library/Perl
/System/Library/Perl/Extras/5.8.6/darwin-thread-multi-2level
/System/Library/Perl/Extras/5.8.6 /Library/Perl/5.8.1 .) at datefind
line 4.
BEGIN failed--compilation aborted at datefind line 4."

So the system couldn't find manip.pm. Currently i am using 5.8.6 in mac
OS X. Would appreciate your help if I can get to know what could be the
solution for it...

Thanks in advance,
Kimi

You need to install the module from CPAN. Setting up the cpan command
takes some effort, but it is worth it because it opens up access to
about 8000 modules that are very helpful and powerful. Once it is set
up, you can just issue the command install Date::Manip, and it will do
it. It will also install any missing prerequisite modules.

The following site is helpful, I think. It includes a Mac OS X section
as well.

http://sial.org/howto/perl/life-with-cpan/

Boyd
 
D

David Squire

John said:
Install it via CPAN.


No, it couldn't find Date/Manip.pm

Also notice that your OS is CaSe SeNSitIVe.

Actually, the OP says he using using Mac OS X, which SomeTimes Is, and
sometimes isn't. Grrr.


DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top