Computing.Net > Forums > Unix > set variables from date conversion

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

set variables from date conversion

Reply to Message Icon

Name: ShapeShifter224
Date: January 25, 2005 at 10:05:09 Pacific
OS: hp-ux
CPU/Ram: unknown
Comment:

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 script

I need to set new_YY, new_DD, new_MM from the yesterdays_date_str.

Brenda



Sponsored Link
Ads by Google

Response Number 1
Name: vgersh99
Date: January 25, 2005 at 10:35:48 Pacific
Reply:

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')


0

Response Number 2
Name: ShapeShifter224
Date: January 25, 2005 at 11:02:35 Pacific
Reply:

thank you vgersh99 :)

how would you set YY to just be YY not CCYY?

thank you so much for your help!

Brenda


0

Response Number 3
Name: vgersh99
Date: January 25, 2005 at 11:17:47 Pacific
Reply:

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


0

Response Number 4
Name: thepubba
Date: January 25, 2005 at 13:30:05 Pacific
Reply:

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 $Date

typeset -R4 TheYear=$Date
typeset -L4 MonthYear=$Date

print $TheYear
print $MonthYear

typeset -R2 TheMonth=$MonthYear
typeset -L2 TheDate=$MonthYear
typeset -R2 TheShortYear=$TheYear

print $TheMonth
print $TheDate
print $TheShortYear

See 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
24

Jerry Lemieux



0

Response Number 5
Name: vgersh99
Date: January 25, 2005 at 13:56:16 Pacific
Reply:

agree with all of the above!

or [in ksh]:

DateString=01242005

month="${DateString%${DateString#??}}"
year="${DateString#${DateString%??}}"

vlad
+
#include<disclaimer.h> ----+


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

AIX problem TOO MANY PROC... Tranfer Unix Files



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: set variables from date conversion

Script: Read file to set variables www.computing.net/answers/unix/script-read-file-to-set-variables/3849.html

Export variables from awk www.computing.net/answers/unix/export-variables-from-awk/7261.html

variables from one file to another www.computing.net/answers/unix/variables-from-one-file-to-another/7378.html