rvsBhanu
asked on
how to find out yesterday's date in mmmddyy format in UNIX/PERL ?
what command/script is used to find out yesterday's date in UNIX/PERL ?
print strftime("%m%d%Y", localtime(time - 3600*24));
Below is part of a code I wrote before, I believe it will work as a unix shell script.
#! /usr/bin/ksh
# ydate.sh
# Today is
T=`date +'%m-%d-%Y'`
# Set month year & date
M=`echo $T | cut -d'-' -f1`
D=`echo $T | cut -d'-' -f2`
Y=`echo $T | cut -d'-' -f3`
# Test for first day of the year first
# Test if first of March
# Test if leap year
# Test if previous month had 31/30 day
if test $D -eq 1
then
if test $M -eq 1
then
Y=`expr $Y - 1`
M=12
D=31
echo $M/$D/$Y
exit
fi
if test $M -eq 3
then
M=`expr $M - 1`
cal $M $Y | tail +2 | grep -q 29
if test $? -eq 0
then
DAY=29
else
DAY=28
fi
else
M=`expr $M - 1`
cal $M $Y | tail +2 | grep -q 31
if test $? -eq 0
then
D=31
else
D=30
fi
fi
else
D=`expr $D - 1`
fi
echo "$M/$D/$Y"
My solution is Perl, by the way. I did not note that, but I see the question is multi-zone now.
use Date::Manip;
my $date = UnixDate("Yesterday",'%b%d%y');
print "date=$date\n";
#Or, if you don't want to use Date::Manip, you can do this
#This will could be incorrect around daylight savings time though
my $date = strftime("%b%d%Y", localtime(time - 3600*24));
print "date=$date\n";
ASKER
well my script looks like this,but it gives the error as I do not have GNU date :
-------------------------- ---------- ---------- ---------- ---------- ---
#!/bin/ksh
filename="file1.d"$(date +"%Y%m%d")
./script1 $filename
echo "script1 completed"
ydate=$(date -d yesterday +"%b%d%y")
./script2 $ydate
echo "script2 completed"
#
-------------------------- ---------- ---------- ---------- ---------- ----
here the script1 completes successfully,but I get error while script2 is executing.
error:
date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
enter date suffix for the filename in the format mmmyy..
-------------------------- ---------- ---------- ---------- ---------- ---
can you please tell me if the above resolution be incorporated in this ?And if so,how ? Also I would like yesterday's date to be displayed as mmmddyy where mmm is the first 3 letters of the name of the month.
Please suggest ?
--------------------------
#!/bin/ksh
filename="file1.d"$(date +"%Y%m%d")
./script1 $filename
echo "script1 completed"
ydate=$(date -d yesterday +"%b%d%y")
./script2 $ydate
echo "script2 completed"
#
--------------------------
here the script1 completes successfully,but I get error while script2 is executing.
error:
date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
enter date suffix for the filename in the format mmmyy..
--------------------------
can you please tell me if the above resolution be incorporated in this ?And if so,how ? Also I would like yesterday's date to be displayed as mmmddyy where mmm is the first 3 letters of the name of the month.
Please suggest ?
If you don't care about DST:
ydate=`perl -MPOSIX -e 'print strftime("%b%d%y", localtime(time-86400))'`
If you care about DST
perl -MDate::print UnixDate("Yesterday", "%b%d%y")'
ASKER
So would this work for shell scripting also(the one I have written),or PERL ?
Also what changes would then be required at the time of DST ?
Also what changes would then be required at the time of DST ?
ASKER
or if you can modify my script to be in PERL so that I can incorporate this..?would be great...
The code I gave in post 24863395 is shell script code. It uses perl to generate the string. The command I gave in post 24863441 is a perl command. It would have to be enclosed in backticks to be have the output go to a variable in a shell script.
Using (time-86400) subtracts 1 normal day (24 hours of 60 minutes each with 60 seconds per each minute) from the current time. The problem with this is that for daylight savings time, a day could be 23 hours or 25 hours. Using this code within that 1 hour window on those days would give the wrong results.
Using the Date::Manip and "yesterday" does not have this problem.
Here is your code updated to get the yesterday from perl.
Using (time-86400) subtracts 1 normal day (24 hours of 60 minutes each with 60 seconds per each minute) from the current time. The problem with this is that for daylight savings time, a day could be 23 hours or 25 hours. Using this code within that 1 hour window on those days would give the wrong results.
Using the Date::Manip and "yesterday" does not have this problem.
Here is your code updated to get the yesterday from perl.
#!/bin/ksh
filename="file1.d"$(date +"%Y%m%d")
./script1 $filename
echo "script1 completed"
ydate=`perl -MDate::print UnixDate("Yesterday", "%b%d%y")'`
./script2 $ydate
echo "script2 completed"
ASKER
when I run the below script,it gives me the o/p as it is :
script :
-------------------------- ---------- ---------- ---------- ---
#!/bin/ksh
ydate='perl -MDate::print UnixDate("Yesterday","%b%d %y")'
echo $ydate
#
-------------------------- ---------
o/p:
perl -MDate::print UnixDate("Yesterday","%b%d %y")
-------------------------- ---------- ---------- ---------- ---------- -
can you figure out what can be the issue here ?
script :
--------------------------
#!/bin/ksh
ydate='perl -MDate::print UnixDate("Yesterday","%b%d
echo $ydate
#
--------------------------
o/p:
perl -MDate::print UnixDate("Yesterday","%b%d
--------------------------
can you figure out what can be the issue here ?
ydate='perl -MDate::print UnixDate("Yesterday","%b%d %y")'
should be
ydate=`perl -MDate::Manip -e 'print UnixDate("Yesterday","%b%d %y")'`
note `` not ''
should be
ydate=`perl -MDate::Manip -e 'print UnixDate("Yesterday","%b%d
note `` not ''
ASKER
Is there any sp. feature that is required to be installed on the UNIX box to be able to use manip command or is it inbuilt ?
cpan Date::Manip
You will need to have the Date::Manip module installed. The command ozo gave:
cpan Date::Manip
will install this module. If it isn't installed, and you don't want to install it, you can use the other command I gave. It will work the same, with the exception of the DST problem I mentioned.
cpan Date::Manip
will install this module. If it isn't installed, and you don't want to install it, you can use the other command I gave. It will work the same, with the exception of the DST problem I mentioned.
To handle DST without Date::Manip, you could do something like
ydate=`perl -MPOSIX -e 'print strftime"%b%d%y", localtime time-3600*(12+(localtime)[ 2])'`
ydate=`perl -MPOSIX -e 'print strftime"%b%d%y", localtime time-3600*(12+(localtime)[
ASKER
this is my current script:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---
#!/bin/ksh
ydate='perl -MDate::Manip -e 'print UnixDate("Yesterday","%b%d %y")''
echo $ydate
-------------------------- ---------- ---------- ---------- ---------- ---------- --
error:
syntax error at line 2 : `(' unexpected
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Can you pls chk this and let me know what needs to be done for this ?
--------------------------
#!/bin/ksh
ydate='perl -MDate::Manip -e 'print UnixDate("Yesterday","%b%d
echo $ydate
--------------------------
error:
syntax error at line 2 : `(' unexpected
--------------------------
Can you pls chk this and let me know what needs to be done for this ?
ASKER
the other one also threw an error.Here is the script :
#!/bin/ksh
ydate='perl -MPOSIX -e 'print strftime"%b%d%y",localtime time-3600*(12+(localtime)[ 2])''
echo $ydate
-------------------------- ---------- ---------- ---------- ------
error:
strftime%b%d%y,localtime: not found
-------------------------- ---------- ---------- ---------- --------
Can someone determine the issue here and let me know if there is any syntax problem somewhere ?
Also is there a way to determine if manip is there on the box or not ?
#!/bin/ksh
ydate='perl -MPOSIX -e 'print strftime"%b%d%y",localtime
echo $ydate
--------------------------
error:
strftime%b%d%y,localtime: not found
--------------------------
Can someone determine the issue here and let me know if there is any syntax problem somewhere ?
Also is there a way to determine if manip is there on the box or not ?
You are using forward quote ' character, when you need to be using backtick ` character. The backtick is the key to the left of the "1" on a keyboard.
ydate=`perl -MDate::Manip -e 'print UnixDate("Yesterday","%b%d%y")'`
ydate=`perl -MPOSIX -e 'print strftime"%b%d%y", localtime time-3600*(12+(localtime)[2])'`
ASKER
Script:
-------------------------- ---------- ------
#!/bin/ksh
ydate=`perl -MDate::Manip -e 'print UnixDate("Yesterday","%b%d %y")'`
echo $ydate
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -------
Error:
Can't locate Date/Manip.pm in @INC (@INC contains: /usr/perl5/5.6.1/lib/sun4- solaris-64 int /usr/perl5/5.6.1/lib /usr/perl5/site_perl/5.6.1 /sun4-sola ris-64int /usr/perl5/site_perl/5.6.1 /usr/perl5/site_perl /usr/perl5/vendor_perl/5.6 .1/sun4-so laris-64in t /usr/perl5/vendor_perl/5.6 .1 /usr/perl5/vendor_perl .).
BEGIN failed--compilation aborted.
-------------------------- ---------- ---------- ---------- --------
However,
#!/bin/ksh
ydate=`perl -MPOSIX -e 'print strftime"%b%d%y",localtime time-3600*(12+(localtime)[ 2])'`
echo $ydate
-------------------------- ---------- ---------- ---------- ------
This works fine.But jus wanted to confirm that would this be taking the DST into account or there is something else that I need to do ?
--------------------------
#!/bin/ksh
ydate=`perl -MDate::Manip -e 'print UnixDate("Yesterday","%b%d
echo $ydate
--------------------------
Error:
Can't locate Date/Manip.pm in @INC (@INC contains: /usr/perl5/5.6.1/lib/sun4-
BEGIN failed--compilation aborted.
--------------------------
However,
#!/bin/ksh
ydate=`perl -MPOSIX -e 'print strftime"%b%d%y",localtime
echo $ydate
--------------------------
This works fine.But jus wanted to confirm that would this be taking the DST into account or there is something else that I need to do ?
The first error is because you do not have Date::Manip installed. You could install it with (run as root):
cpan Date::Manip
If the second is working, you can use that. It will take DST into account.
cpan Date::Manip
If the second is working, you can use that. It will take DST into account.
ASKER
cool...thanks...
jus a small clarification...will the second script take the 1st of the month,leap years and DST into account ?
ie.ydate=`perl -MPOSIX -e 'print strftime"%b%d%y",localtime time-3600*(12+(localtime)[ 2])'`
I mean is there any specific instance where it fails ?though I hope it doesn't...
jus a small clarification...will the second script take the 1st of the month,leap years and DST into account ?
ie.ydate=`perl -MPOSIX -e 'print strftime"%b%d%y",localtime
I mean is there any specific instance where it fails ?though I hope it doesn't...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.