Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have converted a given date to julian date, done some calculations (add, subtract,etc)and have converted the result back to gregorian date - but I can not figure out how to split the result back into the variables new_YY new_MM new_DD
In this example I am working with yesterday , but in other calcs it will be some given day - 60 or whatever. I have the calcs ok, but I just can not seem to figure out how to take the result and set it to new_YY, new_DD, new_MM.
Can someone help me out? Thanks :)
#!/bin/ksh
>
> TY=$(date '+%Y') # Year for today - MUST be YYYY
> TM=$(date '+%m') # Month for today
> TD=$(date '+%d') # Day for today
>
> JD=$(get_astro_JD TD TM TY) # today's Astro-Julian date
> # yesterday's date
> yesterdays_date_str=$(get_greg_from_JD $((JD-1)) )
> # parse yesterdays date string
> set - $(echo $yesterdays_date_str)
> echo $1 # month - this gives 01242005 MMDDYYYY
> echo $2 # day - this is not working
> echo $3 # year - this is not working
> # end scriptI need to set new_YY, new_DD, new_MM from the yesterdays_date_str.
Brenda

new_YY=$(echo ${yesterdays_date_str} | sed -e 's/^.*\(....\)$/\1/g')
new_DD=$(echo ${yesterdays_date_str} | sed -e 's/^.*\(..\)....$/\1/g')
new_MM=$(echo ${yesterdays_date_str} | sed -e 's/^\(..\).*/\1/g')

thank you vgersh99 :)
how would you set YY to just be YY not CCYY?
thank you so much for your help!
Brenda

Since you're using the ksh, you can use the typeset built-in to cut the string into 2 pieces. An example script is:
#!/bin/ksh
Date=01242005
print $Datetypeset -R4 TheYear=$Date
typeset -L4 MonthYear=$Dateprint $TheYear
print $MonthYeartypeset -R2 TheMonth=$MonthYear
typeset -L2 TheDate=$MonthYear
typeset -R2 TheShortYear=$TheYearprint $TheMonth
print $TheDate
print $TheShortYearSee if you can incorporate the typeset into your functions. You avoid having to make a call outside the shell (in the solution, it is to sed) when it is not necessary. Additionally, when you use Korn shell, you don't need to use echo (which is also an external function). Use print, which is a shell built-in just like the typeset command.
The sed command is very powerful and does the job. However, for trival tasks such as what you are trying to do, it is overkill. Additionally, if you are using a newer Korn shell (such as ksh93), you can easily cut up a string as follows:
DateString=01242005
TheMonth=${DateString:0:2}
TheDay=${DateString:2:2}
TheYear4=${DateString:4:4}
TheYear2=${DateString:6:2}print $TheYear2
05
print $TheYear4
2005
print $TheMonth
01
print $TheDay
24Jerry Lemieux

agree with all of the above!
or [in ksh]:
DateString=01242005
month="${DateString%${DateString#??}}"
year="${DateString#${DateString%??}}"vlad
+
#include<disclaimer.h> ----+

![]() |
AIX problem TOO MANY PROC...
|
Tranfer Unix Files
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |