find if the date is the today date, in Perl
find if the date is the today date, in Perl
(OP)
Hi all, i am new to perl.
i have a text file that contains the date in only one row.
I want to check if the date inside this file is the today date or not.
The file contains the date in this form:
file_save = 20120628150005
the first 8 numbers( 20120628 ) indicates the date, the other numbers the hour (150005)
Thank you very much!
PS: i have to do this in a Perl script
i have a text file that contains the date in only one row.
I want to check if the date inside this file is the today date or not.
The file contains the date in this form:
file_save = 20120628150005
the first 8 numbers( 20120628 ) indicates the date, the other numbers the hour (150005)
Thank you very much!
PS: i have to do this in a Perl script
RE: find if the date is the today date, in Perl
What have you tried so far ?
Feherke.
http://feherke.github.com/
RE: find if the date is the today date, in Perl
#!/usr/bin/perl
use strict;
use warnings;
my ( $sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst ) = localtime(time()- 24*60*60 );
$year += 1900;
$mon += 1;
if( $mon < 9 ){
$mon = "0" . $mon;
}
if( $mday < 9 ){
$mday = "0" . $mday;
}
my $date = "$year$mon$mday$hour$min$sec";
RE: find if the date is the today date, in Perl
Interesting. I expected to see some file operations first, where you are reading in the text file.
Then I understood that you are interested in today. So why are subtracting one day ? And anyway, if you want to compare dates, better skip the time part.
Regarding the formatting, that conditional padding is horrible. Use sprintf instead : $date = sprintf '%04d%02d%02d%02d%02d%02d',$year,$mon,$mday,$hour,$min,$sec;.
Feherke.
http://feherke.github.com/
RE: find if the date is the today date, in Perl
Based on specification, I would solve it like this :
CODE --> Perl
Feherke.
http://feherke.github.com/
RE: find if the date is the today date, in Perl
Can you please help me in another step?
I want to add this:
If the date is the TODAY day, copy this file into another server.
Thank you very very much!!!
RE: find if the date is the today date, in Perl
How is that "another server" connected ?
Feherke.
http://feherke.github.com/
RE: find if the date is the today date, in Perl
I have done this till now, and it seems to work:
#!/usr/bin/perl
use strict;
use warnings;
use Net::SFTP::Foreign;
open FIL, '<monitorFile';
my $line = <FIL>;
close FIL;
my $host = 'host1';
my $password = 'password';
my $user = 'root';
my $sftp = Net::SFTP::Foreign->new(host => 'host1', user => 'root', password => 'password');
my ( $sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst ) = localtime;
$year += 1900;
$mon++;
my $date = sprintf '%04d%02d%02d', $year, $mon, $mday;
if ($line =~ m/ = $date/ ){
$sftp->put("/monitorFile", "/tmp/monitorFile") or die "put failed: " . $sftp->error;
}
Thank you very very much for you support!!!!!!!!!!!!