Sorry to be a bit late to the party, but that does seem rather fragile. Also, 
from your original post, you seem to make hard work of formatting the date 
strings. The following suggestion adresses both points.

use strict;
use warnings;
use constant {SECS_PER_DAY => 24 * 60 * 60};
use POSIX qw{strftime};

my $startDate = strftime "%Y-%m-%d_", localtime();
my $endDate = strftime "%Y-%m-%d_", localtime(time + 2 * SECS_PER_DAY);
my $dateRE = qr{\d{4}-\d{2}-\d{2}_};

while (my $line = <DATA>) {
    if ($line =~ m/^start_date/) {
        $line =~ s/$dateRE/$startDate/g;
    }
    elsif ($line =~ /^end_date/) {
        $line =~ s/$dateRE/$endDate/g;
    }
    print $line;
}

__DATA__
First line
start_date = '2012-04-29_00:00:00', '2012-04-29_00:00:00', 
'2012-04-29_00:00:00',
end_date   = '2012-05-01_00:00:00', '2012-05-01_00:00:00', 
'2012-05-01_00:00:00',
Last line

HTH

--
Brian Raven

From: activeperl-boun...@listserv.activestate.com 
[mailto:activeperl-boun...@listserv.activestate.com] On Behalf Of zilore mumba
Sent: 01 May 2012 08:22
To: zilore mumba; $Bill Luebkert
Cc: activeperl@listserv.ActiveState.com
Subject: Re: help regex replacement

Thanks very much all who spent precious time looking at my problem. Below is 
how I got round the problem, i.e. the first three lines are not to be changed 
hence copied as they are. Then the lines with 'start_date' and end_date are 
inserted and then the rest of the file is copied unchanged.
I really appreciate the spirit of assisting.
Zilore

************************************************************************************************************
* # open input and output files.
*
*   open (IN, "< namelist.wps.dailyGFS") or die "open 'namelist.wps.dailyGFS: 
failed $! ($^E)";
*   open (OUT, "> namelist.wps") or die "open 'namelist.wps: failed $! ($^E)";
*
*     print OUT scalar(<IN>) for 1..3;
*     print OUT " start_date = 
'${syear}-${smonth}-${sday}_00:00:00','${syear}-${smonth}-${sday}_00:00:00','${syear}-${smonth}-${sday}_00:00:00',\n";
*     print OUT " end_date = 
'${eyear}-${emonth}-${eday}_00:00:00','${eyear}-${emonth}-${eday}_00:00:00','${eyear}-${emonth}-${eday}_00:00:00',\n";
*     print scalar(<IN>);
*     print scalar(<IN>);
*     print OUT scalar(<IN>) for 6..37;
*   close IN;
*   close OUT;
******************************************************************************************************************

________________________________
From: zilore mumba <zmu...@yahoo.com<mailto:zmu...@yahoo.com>>
To: zilore mumba <zmu...@yahoo.com<mailto:zmu...@yahoo.com>>; $Bill Luebkert 
<dbec...@roadrunner.com<mailto:dbec...@roadrunner.com>>
Cc: 
"activeperl@listserv.ActiveState.com<mailto:activeperl@listserv.ActiveState.com>"
 
<activeperl@listserv.ActiveState.com<mailto:activeperl@listserv.ActiveState.com>>
Sent: Tuesday, May 1, 2012 12:43 AM
Subject: Re: help regex replacement

T add a bit more precision, the lines must appear as they are because the 
program which reads the file will look for "start_date=followed by the date". I 
was looking, in perl, for something like "replace line starting in, say 
/^start_date .. and ending in ...'2012-04-29_00:00:00',$/
I hope this is possible.
Zilore

________________________________
From: zilore mumba <zmu...@yahoo.com<mailto:zmu...@yahoo.com>>
To: $Bill Luebkert <dbec...@roadrunner.com<mailto:dbec...@roadrunner.com>>
Cc: 
"activeperl@listserv.ActiveState.com<mailto:activeperl@listserv.ActiveState.com>"
 
<activeperl@listserv.ActiveState.com<mailto:activeperl@listserv.ActiveState.com>>
Sent: Tuesday, May 1, 2012 5:31 AM
Subject: Re: help regex replacement

Thanks very much Bill, and also for the assistance to me over the years. Your 
suggestion does not do exactly what I want to do. The start_date and end_date 
are in a file, in the middle of other text. The idea is to replace daily the 
year,month and day to the current year,month and dayand on end_date to replace 
to tomorrow or after tomorrow, The fields may appear twice or yhree times 
because the model has nests in it which all use the same times.
I hope this is clear.

________________________________
From: $Bill Luebkert <dbec...@roadrunner.com<mailto:dbec...@roadrunner.com>>
To: zilore mumba <zmu...@yahoo.com<mailto:zmu...@yahoo.com>>
Cc: 
"activeperl@listserv.ActiveState.com<mailto:activeperl@listserv.ActiveState.com>"
 
<activeperl@listserv.ActiveState.com<mailto:activeperl@listserv.ActiveState.com>>
Sent: Tuesday, May 1, 2012 12:02 AM
Subject: Re: help regex replacement

On 04/30/2012 14:28, zilore mumba wrote:
> sample input
> start_date = 
> '2012-04-29_00:00:00','2012-04-29_00:00:00','2012-04-29_00:00:00',
> end_date = '2012-05-01_00:00:00','2012-05-01_00:00:00','2012-05-01_00:00:00',
>
> The model would have run on 29th May, to produce a 48hour forecast to 1sr May 
> 2012.
>> When I run it on 30th May it should produce a 48hour forecast to 2nd May, 
>> i.e. change the entries above so that start_date is 30th and end_date is 2nd 
>> May as below.

> start_date = 
> '2012-04-30_00:00:00','2012-04-30_00:00:00','2012-04-30_00:00:00',
> end_date = '2012-05-02_00:00:00','2012-05-02_00:00:00','2012-05-02_00:00:00',
>
> zilore mumba schrieb am 30.04.2012 um 10:14 (-0700):
>  > I have to automate a modle namelist by replacing the start and end
>  > date of the model run as per lines below.

I have no idea where this data is or what you are doing, but it's an easy matter
to take a date and add 86400 to it and format it back out again.

use strict;
use warnings;
use POSIX;
use Time::Local;

# assuming you have this data in $start_date:
my $start_date = '2012-04-29_00:00:00';

# and you want to add 1 day to it

# just get date portion because the time doesn't appear to be used (always == 0)
# you could split on _ and work both date and time if needed
(my $date = $start_date) =~ s/_.*$//;

# OK, now convert it to epoch time
my @D = split /-/, $date;
# OK, we now have the year, month and day in @D

# let's convert it to epoch (you could use timegm if UTC time rather than local)
# I'm hard coding the time to 0 per example
my $epoch = timelocal 0, 0, 0, $D[2], $D[1]-1, $D[0]-1900;

# now let's add a day to it
$epoch += 86400;

# OK, all done, let's format a new date-time string
my @tm = localtime $epoch;    # get it back from epoch to its pieces
my $new_start_date = strftime "%Y-%m-%d_%H:%M:%S\n", @tm;    # format it

print "start_date=$start_date, new start_date=$new_start_date\n";

__END__


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com<mailto:ActivePerl@listserv.ActiveState.com>
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com<mailto:ActivePerl@listserv.ActiveState.com>
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

________________________________
Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are 
not the intended recipient or have received this e-mail in error, please advise 
the sender immediately by reply e-mail and delete this message and any 
attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to