The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sed: zero-padding dates (or: convert d/m/yyyy to dd/mm/yyyy) jgrogan Shell Programming and Scripting 3 07-30-2009 01:12 AM
Date difference between 2 dates in 'yyyy-mm-dd hh:mm:ss' format muay_tb Shell Programming and Scripting 4 04-07-2009 02:16 PM
Format date from MM/DD/YYYY to YYYYMMDD ChicagoBlues UNIX for Dummies Questions & Answers 5 03-24-2009 07:10 PM
Need script to generate all the dates in DDMMYY format between 2 dates frozensmilz Shell Programming and Scripting 2 01-29-2009 06:06 AM
sed to display date in dd/mm/yyyy format sars Shell Programming and Scripting 2 02-15-2007 03:32 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-01-2009
pavan_test pavan_test is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 53
fetch dates for last 36 days in format yyyy-mm-dd

can anyone please suggest me some ideas for writing a korn shell script which will go back to 36 days from current day and print each day (incremented by 1) in the format yyyy-mm-dd until the current day.

Thanks
Mark
  #2 (permalink)  
Old 10-01-2009
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
There are a number of articles regarding date arithmetic on the site:

try Yesterdays Date/Date Arithmetic

from the FAQs - you may find your answer (or something close) in there.

What have you tried so far ? Post any code you may have and you're likely to get some good assistance.

Cheers
  #3 (permalink)  
Old 10-01-2009
pavan_test pavan_test is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 53
fetch dates for last 36 days in format yyyy-mm-dd

This is the code i have so far. for now the loop is for 5 days. later i wish to update it to 36 days.


Code:
#!/bin/ksh

set -x
i=1
 while [ $i -le 5 ]; do
_day=`date +%Y%m%d`
newday=`expr $_day - $i`
echo $newday
echo "================================="
i=`expr $i + 1`
echo $i
done

but i have a problem, expr 20091001 - 2 then i see this newday=20090999

Can anyone please suggest me.

Thanks
Mark

Last edited by vgersh99; 10-01-2009 at 01:02 PM.. Reason: code tags, PLEASE!
  #4 (permalink)  
Old 10-01-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,130
The previously suggested FAQ link and the forum 'Search' capability should give you a head start.
Have you looked into those?

---------- Post updated at 12:05 PM ---------- Previous update was at 12:05 PM ----------

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
  #5 (permalink)  
Old 10-01-2009
sunilk1303's Avatar
sunilk1303 sunilk1303 is offline
Registered User
  
 

Join Date: Aug 2009
Location: Los Angeles
Posts: 3
heres a simple way:-

Code:
#!/bin/ksh
set -x
i=1
 while [ $i -le 5 ]; do
date -d "`date +%Y`-01-01 +$(( `date +"%j"` - $i ))days" +%Y-%m-%d
i=`expr $i + 1`
echo $i
done

i have used a simple trick to convet the current calender date into julian date, substract it by 1, and convert back it to the calendar date
  #6 (permalink)  
Old 10-02-2009
thegeek thegeek is offline
Registered User
  
 

Join Date: Apr 2009
Location: /usr/bin/vim
Posts: 455
one another way,


Code:
date --date="36 days ago"  +"%Y-%m-%d"

Use a variable and reduce the 36 upto required num, and proceed.


As, I have no experience in writing korn shell script !!! Write the above suggested by your own.
  #7 (permalink)  
Old 10-02-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,784
the geek -

Your method is good as long as the OP has GNU date installed on his machine.

You can also try perl.

Code:
#!/bin/ksh
# will run on Solaris with the date/time format %s problem.

end=$( perl -e ' print time;' )
start=$(( end - ( 86400 * 36) ))  # 36 days in the past
while [[  $end -ge $start  ]]
do
     perl -e 'use POSIX qw(strftime);
              $mt = strftime "%a %b %e %H:%M:%S %Y", localtime($ARGV[0]); 
              print $mt,"\n";' $start
     start=$(( $start + 86400 ))
done

Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0