×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

find if the date is the today date, in Perl

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

RE: find if the date is the today date, in Perl

(OP)
i have written only the part that is related to the format of my date. sorry, but i am new to 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

Hi

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

Hi

Based on specification, I would solve it like this :

CODE --> Perl

#!/usr/bin/perl

use strict;
use warnings;

open FIL, '<arrals.txt';
my $line = <FIL>;
close FIL;

my ( $sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst ) = localtime;
$year += 1900;
$mon++;
my $date = sprintf '%04d%02d%02d', $year, $mon, $mday;

print $line =~ m/ = $date/ ? 'Yepp, that is today' : 'Not today'; 

Feherke.
http://feherke.github.com/

RE: find if the date is the today date, in Perl

(OP)
Thank you very much!! that was great!

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

Hi

Quote (arrals)

copy this file into another server.
How is that "another server" connected ?

Feherke.
http://feherke.github.com/

RE: find if the date is the today date, in Perl

(OP)
Both server are in cluster, in a virtual environment.

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!!!!!!!!!!!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close