?
Solved

how to find out yesterday's date in mmmddyy format in UNIX/PERL ?

Posted on 2009-07-15
23
Medium Priority
?
1,310 Views
Last Modified: 2013-12-26
what command/script is used to find out yesterday's date in UNIX/PERL ?
0
Comment
Question by:rvsBhanu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 3
  • +2
23 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24862065
print strftime("%m%d%Y", localtime(time - 3600*24));

0
 

Expert Comment

by:aschuete
ID: 24862471
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"

Open in new window

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24862496
My solution is Perl, by the way. I did not note that, but I see the question is multi-zone now.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 39

Expert Comment

by:Adam314
ID: 24863038

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";

Open in new window

0
 

Author Comment

by:rvsBhanu
ID: 24863364
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 ?
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24863395
If you don't care about DST:
ydate=`perl -MPOSIX -e 'print strftime("%b%d%y", localtime(time-86400))'`

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
ID: 24863441
If you care about DST
perl -MDate::print UnixDate("Yesterday", "%b%d%y")'

Open in new window

0
 

Author Comment

by:rvsBhanu
ID: 24863448
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 ?
0
 

Author Comment

by:rvsBhanu
ID: 24863458
or if you can modify my script to be in PERL so that I can incorporate this..?would be great...
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24863519
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.
#!/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"

Open in new window

0
 

Author Comment

by:rvsBhanu
ID: 24863711
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 ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 24865006
ydate='perl -MDate::print UnixDate("Yesterday","%b%d%y")'
should be
ydate=`perl -MDate::Manip -e 'print UnixDate("Yesterday","%b%d%y")'`

note `` not ''


0
 

Author Comment

by:rvsBhanu
ID: 24865682
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 ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 24866092
cpan Date::Manip
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24866758
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 24866852
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])'`
0
 

Author Comment

by:rvsBhanu
ID: 24869066
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 ?
0
 

Author Comment

by:rvsBhanu
ID: 24869148
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 ?
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24869187
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])'`

Open in new window

0
 

Author Comment

by:rvsBhanu
ID: 24869491
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-64int /usr/perl5/5.6.1/lib /usr/perl5/site_perl/5.6.1/sun4-solaris-64int /usr/perl5/site_perl/5.6.1 /usr/perl5/site_perl /usr/perl5/vendor_perl/5.6.1/sun4-solaris-64int /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 ?
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24869544
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.
0
 

Author Comment

by:rvsBhanu
ID: 24870438
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...
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 24870531
Yes, it'll handle all of those.  It should be correct in all cases.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question