Help to get date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to get date
# 1  
Old 01-28-2009
Help to get date

Hi,

I need some help... i mean can someone give some idea on how to implement what i want to do...

I have script called Mail.ksh which calls another script called read.ksh. read.ksh appends data to time.out file...This Mail.ksh runs everyday say 9:00AM on cron... We have like 50 Jobs on cron and read.ksh collects data form these 50 jobs and appends the data to time.out

time.out file looks like this
Code:
 
.
.
.
.
20090123,00:02:33,00:00:35,8134,00:01:42,00:01:32,02:02:08,321055, 
20090124,00:02:33,00:00:35,9345,00:01:42,00:01:32,02:02:08,321055, 
20090125,00:02:33,00:00:35,8783,00:01:42,00:01:32,02:02:08,321055, 
20090126,00:02:33,00:00:35,44554,00:01:42,00:01:32,02:02:08,321055,

The mail.ksh calculates the current mantaince date using the below code
Code:
 
julian2date() # julianday
{
typeset -i day month year tmpday
julianday=$1
((tmpday = julianday - 1721119))
((centuries = (4 * tmpday - 1) / 146097))
((tmpday += centuries - centuries/4))
((year = (4 * tmpday - 1) / 1461))
((tmpday -= (1461 * year) / 4))
((month = (10 * tmpday - 5) / 306))
((day = tmpday - (306 * month + 5) / 10))
((month += 2))
((year += month/12))
((month = month % 12 + 1))
MONTH_LEN=$(echo ${month} | awk '{print length($1)}')
if [[ ${MONTH_LEN} = 1 ]]
then
 format_month="0${month}"
else
 format_month=${month}
fi
DAY_LEN=$(echo ${day} | awk '{print length($1)}')
if [[ ${DAY_LEN} = 1 ]]
then
 format_day="0${day}"
else
 format_day=${day}
fi
print $year$format_month$format_day
}
########################################################
# Get the Julian equivalent of the current date, subtract 1 day, convert back to a date
typeset -i LSD_DAY LSD_MONTH LSD_YEAR LSD_JUL CMD_JUL CUR_MAINT_DATE
YYYYMMDD=`date +%Y%m%d`
LSD_DAY=$(echo ${YYYYMMDD} | awk '{print substr($1,7,2)}')
LSD_MONTH=$(echo ${YYYYMMDD} | awk '{print substr($1,5,2)}')
LSD_YEAR=$(echo ${YYYYMMDD} | awk '{print substr($1,1,4)}')
LSD_JUL=$(date2julian ${LSD_DAY} ${LSD_MONTH} ${LSD_YEAR})
(( CMD_JUL = LSD_JUL - 1 ))
CUR_MAINT_DATE=$(julian2date ${CMD_JUL})

Usually the date will be previous daye ie if mail.ksh script is running at 9:00AM on 2009 01 27 then current mantaince date will be 2009 01 26
This is what the above code do.. The Mail.ksh takes the current maint date and pass it to read.ksh when will go a folder where the data of the files generated on current maint date are stored and collect the required data form them and appends the collected data to time.out

Now i want to change this... When ever mail.ksh runs it not only do the process for current maint date but also go back two dats and gets data...
like if its running on 27 then its also runs for data 26 and 25
get the data and place the data in the time.out such a way that it removes previous data and addes this data in the respective dates...
I want it to run 2 days back and remove data in time.out to respective date and place what so ever new data...

Please help me how to approch this problem...

Thanks a lot

-Bhagya
# 2  
Old 01-28-2009
Bhagya,

Try to separate functionality (goals) from mechanism (steps to implement). Similarly, separate the function of performing maintenance from the step of finding the date. Pass the date into the function, and if you want to use another date, just call the function (or script) again with the different date.

Now, the code above is very complicated for doing what the date command already does. And out of curiosity, who needs Julian days? Okay, so you need them. Use the date command like this:
Code:
#Replace the lines above
# LSD_JUL=$(date2julian ${LSD_DAY} ${LSD_MONTH} ${LSD_YEAR})
#With...
LSD_JUL=$(date2julian $(date +"%Y %m %d"))
#and 
# CUR_MAINT_DATE=$(julian2date ${CMD_JUL})
#With...
CUR_MAINT_DATE=$(date +"%Y%m%d")

No need for all that other stuff.
# 3  
Old 01-28-2009
mail.ksh is going to be kept in cron so i cant give the data from out side... i want something like a for loop...
where in code i will give some x=3 so the loop starts two days back calculates date from taking out 2 days form Julian date and calls read.ksh with 2 days back date ....and when it get back it should reamove 1 from x ie x =2 and calculate date and calls read.ksh and then x=1... i want it like this...
Code:
x=3
for (i=x; i<=x; x--) # i want the loop to go from 3 to 2 to 1 and exit when i=0
do
typeset -i LSD_DAY LSD_MONTH LSD_YEAR LSD_JUL CMD_JUL CUR_MAINT_DATE
YYYYMMDD=`date +%Y%m%d`
LSD_DAY=$(echo ${YYYYMMDD} | awk '{print substr($1,7,2)}')
LSD_MONTH=$(echo ${YYYYMMDD} | awk '{print substr($1,5,2)}')
LSD_YEAR=$(echo ${YYYYMMDD} | awk '{print substr($1,1,4)}')
LSD_JUL=$(date2julian ${LSD_DAY} ${LSD_MONTH} ${LSD_YEAR})
(( CMD_JUL = LSD_JUL - i ))
CUR_MAINT_DATE=$(julian2date ${CMD_JUL})

read.ksh ${CUR_MAINT_DATE} >> time.out

is this possible... if so please help me in putting it right order

Last edited by bhagya2340; 01-28-2009 at 01:18 PM..
# 4  
Old 01-28-2009
Quote:
Originally Posted by bhagya2340
Code:
YYYYMMDD=`date +%Y%m%d`
LSD_DAY=$(echo ${YYYYMMDD} | awk '{print substr($1,7,2)}')
LSD_MONTH=$(echo ${YYYYMMDD} | awk '{print substr($1,5,2)}')
LSD_YEAR=$(echo ${YYYYMMDD} | awk '{print substr($1,1,4)}')


There's no need for awk. Populate all three variables with a single call to date:

Code:
eval "$( date +" LSD_DAY=%d LSD_MONTH=%m LSD_YEAR=%Y" )"

There's a library of shell functions for date manipulation at The Dating Game.
# 5  
Old 01-28-2009
Thanks... but organally it was written by someone... i bacially not a unix person... if i change in one ksh they will ask me to change on 65 more khs... i dont want to... let it be as it is till it does the work...

I want to know about the for loop i wrote before... please let me know will it work... if i write it like that.... say me any correction i need in the loop
# 6  
Old 01-29-2009
Quote:
Originally Posted by cfajohnson
[indent]
Code:
eval "$( date +" LSD_DAY=%d LSD_MONTH=%m LSD_YEAR=%Y" )"

CFAJ, you rock.
# 7  
Old 01-29-2009
Quote:
Originally Posted by bhagya2340
Thanks... but organally it was written by someone... i bacially not a unix person... if i change in one ksh they will ask me to change on 65 more khs... i dont want to... let it be as it is till it does the work...
If you change 65 files to include one file, then future changes will require only one file to change. Also, 65 files, assuming each takes a minute to change, will be an hour of your time. How long will you waste on doing it the hard way?? Smilie

Quote:
Code:
for (i=x; i<=x; x--) # i want the loop to go from 3 to 2 to 1 and exit when i=0

I want to know about the for loop i wrote before... please let me know will it work... if i write it like that.... say me any correction i need in the loop
You need:
Code:
for i in 3 2 1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Answers to Frequently Asked Questions

Compare date in .txt with system date and remove if it's lesser than system date

I m working on shell scripting and I m stuck where in my .txt file there is column as expiry date and I need to compare that date with system date and need to remove all the rows where expiry date is less than system date and create a new .txt with update. (1 Reply)
Discussion started by: Stuti
1 Replies

2. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

3. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

4. Shell Programming and Scripting

Script to determine Date,TotalFile,total size of file based on date

I have file listed like below -rw-r--r--+ 1 test test 17M Nov 26 14:43 test1.gz -rw-r--r--+ 1 test test 0 Nov 26 14:44 test2.gz -rw-r--r--+ 1 test test 0 Nov 27 10:41 test3.gz -rw-r--r--+ 1 test test 244K Nov 27 10:41 test4.gz -rw-r--r--+ 1 test test 17M Nov 27 10:41 test5.gz I... (5 Replies)
Discussion started by: krish2014
5 Replies

5. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

6. Shell Programming and Scripting

Check if a date field has date or timestamp or date&timestamp

Hi, In a field, I should receive the date with time stamp in a particular field. But sometimes the vendor sends just the date or the timestamp or correctl the date&timestamp. I have to figure out the the data is a date or time stamp or date&timestamp. If it is date then append "<space>00:00:00"... (1 Reply)
Discussion started by: machomaddy
1 Replies

7. Shell Programming and Scripting

finding date numeral from file and check the validity of date format

hi there I have file names in different format as below triss_20111117_fxcb.csv triss_fxcb_20111117.csv xpnl_hypo_reu_miplvdone_11172011.csv xpnl_hypo_reu_miplvdone_11-17-2011.csv xpnl_hypo_reu_miplvdone_20111117.csv xpnl_hypo_reu_miplvdone_20111117xfb.csv... (10 Replies)
Discussion started by: manas_ranjan
10 Replies

8. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

9. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

10. UNIX for Dummies Questions & Answers

Move A File With Same Date,don't Change The Desitination Dir Date

Assume, I created one file three years back and I like to move the file to some other directory with the old date (Creation date)? Is it possible? Explain? (1 Reply)
Discussion started by: jee.ku2
1 Replies
Login or Register to Ask a Question