Comparing the dates with the current date in perl scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing the dates with the current date in perl scripting
# 1  
Old 04-18-2013
Comparing the dates with the current date in perl scripting

Hi

i have a file containg dates likebelow

Code:
4/30/2013
3/31/2013
4/30/2013
4/16/2013
4/30/2013
4/30/2013
5/30/2013
5/30/2013
4/30/2013
5/30/2013
5/30/2013
3/31/2013


now i want to compare the above dates with current date and i want to display the difference .

eg: take first date in the file i.e 4/30/2013 and current date is 4/18/2013 i want output like 12 days left from today ...... like this iwant all the dates in the file to be compared withe current date and display the difference...


please friends help me

Last edited by radoulov; 04-18-2013 at 05:59 AM..
# 2  
Old 04-18-2013
Code:
$ cat dates.sh
today_seconds=`date "+%s"`
seconds_in_day=86400
while read date; do
  month=`echo $date | cut -d "/" -f 1`
    day=`echo $date | cut -d "/" -f 2`
   year=`echo $date | cut -d "/" -f 3`

  seconds=`date -d "$year-$month-$day" "+%s"`
  diff=`expr $today_seconds - $seconds`
  days=`expr $diff / $seconds_in_day`
  echo days between today and $date = $days
done < dates

Code:
$ ./dates.sh
days between today and 4/30/2013 = -11
days between today and 3/31/2013 = 18
days between today and 4/30/2013 = -11
days between today and 4/16/2013 = 2
days between today and 4/30/2013 = -11
days between today and 4/30/2013 = -11
days between today and 5/30/2013 = -41
days between today and 5/30/2013 = -41
days between today and 4/30/2013 = -11
days between today and 5/30/2013 = -41
days between today and 5/30/2013 = -41
days between today and 3/31/2013 = 18

Code:
$ uname
Linux

# 3  
Old 04-18-2013
hi thank you very much

But i need this to in perl script....
could you please write the script in perl.....please
# 4  
Old 04-18-2013
No, I can't write the script in perl. Sorry about that. For someone who knows perl, it should not be a big deal to translate the logic into perl.
This User Gave Thanks to hanson44 For This Post:
# 5  
Old 04-18-2013
Hi friends, is there any one to help me in perl scripting for the above?
# 6  
Old 04-18-2013
I apologize for not seeing that the post subject clearly said "in perl scripting". Smilie

Isn't there anyone on the forum who does perl scripting who could take a look? The program logic is all there.
# 7  
Old 04-18-2013
I think you had some impossible dates in there.

Code:
#!/usr/bin/perl

use Time::Local;
use POSIX qw(strftime);

while($line=<STDIN>)
{
        my ($mon, $mday, $year)=split(/\//, $line);
        my $timestamp=timelocal(0,0,0,$mday,$mon,$year);
        my $diff=($timestamp - time());
        chomp($line);
        print $line, " is ",int($diff/(60*60*24)), " days away\n";
}

Code:
$ ./tme.pl  <<EOF
4/30/2013
4/30/2013
4/16/2013
4/30/2013
4/30/2013
5/30/2013
5/30/2013
4/30/2013
5/30/2013
5/30/2013
EOF
4/30/2013 is 41 days away
4/30/2013 is 41 days away
4/16/2013 is 27 days away
4/30/2013 is 41 days away
4/30/2013 is 41 days away
5/30/2013 is 72 days away
5/30/2013 is 72 days away
4/30/2013 is 41 days away
5/30/2013 is 72 days away
5/30/2013 is 72 days away

$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

current date - 8 months in perl script

I have a requirement as follows. when i pass a date to the perl script, it has to calculate the current date - 8 months and output the date back to the shell script in date format (YYYY-MM-DD). Current date - 8 months is not constant.. because leap year, and the months jan, mar, may,.... has... (4 Replies)
Discussion started by: kmanivan82
4 Replies

2. Shell Programming and Scripting

Fetch date of 7 years back from current date in Perl

$beginDate = substr(DateCalc("today", "-7Days"),0,8); This fetches the date 7 days back Can I fetch the date before 7 years from todays date in Perl using same syntax Use code tags, see PM. (3 Replies)
Discussion started by: parthmittal2007
3 Replies

3. UNIX for Dummies Questions & Answers

Comparing Output Date to Current System Date

Hi Guys, Anyone who knows how to compare the current date with the a file containing a date, say for example I have a file that looks like this: Command was launched from partition 0. ------------------------------------------------ Executing command in server server6 Fri Dec 16... (7 Replies)
Discussion started by: rymnd_12345
7 Replies

4. UNIX for Dummies Questions & Answers

Comparing two files with datestamp to current date

Hi, I am new to unix and I am stuck on how to compare two .zip file with date stamp in my directory. I need to compare out of the two file which is oldest to current date and unzip it after that done continue to unzip the second zip file. Thanks for your help. (5 Replies)
Discussion started by: lilvi3tboix1
5 Replies

5. Shell Programming and Scripting

Need to capture dates between start date and end date Using perl.

Hi All, Want to get all dates and Julian week number for that date between the start date and end date. How can I achive this using perl? (To achive above functionality, I was connecting to the database from DB server. Need to execute the same script in application server, since databse... (6 Replies)
Discussion started by: Nagaraja Akkiva
6 Replies

6. Shell Programming and Scripting

how to append current date to filename.tgz in perl

i would like to know how to append current date in a filename with .tgz extension. #!/usr/bin/perl my $date = `date + %Y%m%d`; system("sudo mv /tmp/nyucs01_config_backup.tgz /misc/nyucs01_config_backup_$date.tgz"); im getting this error message: sh: line 1: .tgz: command not found (7 Replies)
Discussion started by: linuxgeek
7 Replies

7. UNIX and Linux Applications

sqlite: calculating with dates - compare current date minus 6 months with stored record

Hi I have a table with name, date in format DD.MM.YYYY. I need to something like this (I try to explain in pseudo code) if SYSDATE (current date) minus 6 months > $expiry date print OK else print NOK with $name and $expiry date I know this is possible with Oracle. How to do this... (0 Replies)
Discussion started by: slashdotweenie
0 Replies

8. Shell Programming and Scripting

Comparing current date

Hi, I have start date and end date in the following format. I need to check the current date is greater than the start date and less than the end date. if i use the command date --date "Tue 6:00 AM", it takes next Tues day not the current week's Tues day. Is there a way to get the current Tues... (9 Replies)
Discussion started by: bharathappriyan
9 Replies

9. UNIX for Dummies Questions & Answers

print all dates 100 days from current date

can anyone please suggest me on ideas to write a script which has to go back to 100 days from 'current date' and print the date of each day starting from 100th day to current day. (korn shell please) Thanks Pavan (5 Replies)
Discussion started by: pavan_test
5 Replies

10. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies
Login or Register to Ask a Question