Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find if the date is the today date, in Perl 1

Status
Not open for further replies.

arrals

Technical User
May 4, 2012
5
IT
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 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";
 
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 : [tt][navy]$date[/navy] [teal]=[/teal] sprintf [green]'%04d%02d%02d%02d%02d%02d'[/green][teal],[/teal][navy]$year[/navy][teal],[/teal][navy]$mon[/navy][teal],[/teal][navy]$mday[/navy][teal],[/teal][navy]$hour[/navy][teal],[/teal][navy]$min[/navy][teal],[/teal][navy]$sec[/navy][teal];[/teal][/tt].

Feherke.
[link feherke.github.com/][/url]
 
Hi

Based on specification, I would solve it like this :
Perl:
[gray]#!/usr/bin/perl[/gray]

[b]use[/b] strict[teal];[/teal]
[b]use[/b] warnings[teal];[/teal]

[b]open[/b] FIL[teal],[/teal] [green][i]'<arrals.txt'[/i][/green][teal];[/teal]
[b]my[/b] [navy]$line[/navy] [teal]=[/teal] [green][i]<FIL>[/i][/green][teal];[/teal]
[b]close[/b] FIL[teal];[/teal]

[b]my[/b] [teal]([/teal] [navy]$sec[/navy][teal],[/teal][navy]$min[/navy][teal],[/teal][navy]$hour[/navy][teal],[/teal][navy]$mday[/navy][teal],[/teal][navy]$mon[/navy][teal],[/teal][navy]$year[/navy][teal],[/teal][navy]$wday[/navy][teal],[/teal][navy]$yday[/navy][teal],[/teal][navy]$isdst[/navy] [teal])[/teal] [teal]=[/teal] localtime[teal];[/teal]
[navy]$year[/navy] [teal]+=[/teal] [purple]1900[/purple][teal];[/teal]
[navy]$mon[/navy][teal]++;[/teal]
[b]my[/b] [navy]$date[/navy] [teal]=[/teal] [b]sprintf[/b] [green][i]'%04d%02d%02d'[/i][/green][teal],[/teal] [navy]$year[/navy][teal],[/teal] [navy]$mon[/navy][teal],[/teal] [navy]$mday[/navy][teal];[/teal]

[b]print[/b] [navy]$line[/navy] [teal]=~[/teal] [b]m[/b][fuchsia]/ = [/fuchsia][navy]$date[/navy][fuchsia]/[/fuchsia] [teal]?[/teal] [green][i]'Yepp, that is today'[/i][/green] [teal]:[/teal] [green][i]'Not today'[/i][/green][teal];[/teal]


Feherke.
[link feherke.github.com/][/url]
 
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!!!
 
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!!!!!!!!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top