How to get the consecutive last 10 week day date using UNIX ksh shell scripting?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the consecutive last 10 week day date using UNIX ksh shell scripting?
# 1  
Old 12-31-2013
How to get the consecutive last 10 week day date using UNIX ksh shell scripting?

Hi,

i am writing a ksh shell script to check the last month end date whether it is falling in last 10 week day date, I am not sure How to use "Mr. Perderabo's date calculator", Could you Please let me know how to use to get my requirement, I tried my own script but duplicate week day and duplicate weekend dates are coming , i know how to trim , but i am not sure this logic is right and it is right approach.

Code:
rm -f test_file.txt
counter=0
while [[ $counter -le 9 ]]
do
#if [[ `date -d "${counter} day ago" +"%w"` -gt 0 && `date -d "${counter} day ago" +"%w"` -lt 6 ]]; then
#date -d "${counter} day ago" +"%Y%m%d" >> test_file.txt
if [[ `date -d "${counter} day ago" +"%w"` -eq 0 ]];then
#date -d "${counter} day ago" -d "1 day ago" +"%Y%m%d" >> test_file.txt
date -d "(${counter}+1) day ago" +"%Y%m%d" >> test_file.txt
elif [[ `date -d "${counter} day ago" +"%w"` -eq 6 ]];then
#date -d "${counter} day ago" -d "2 day ago" +"%Y%m%d" >> test_file.txt
date -d "(${counter}+2) day ago" +"%Y%m%d" >> test_file.txt
else
date -d "${counter} day ago" +"%Y%m%d" >> test_file.txt
fi
let counter=counter+1
done

Please Advise.

Thanks,
Regards,
karthik
# 2  
Old 12-31-2013
Facebook

An example would be helpful as it is hard to figure out from you script exactly what you are looking for.
# 3  
Old 12-31-2013
Thanks, My requirement is say month end date 20131231 is falling as one of last 10 consecutive dates,

say today (current date) is 20140101 count as 0
20131231 count as 1 , so this is falling in last 10 consecutive dates
20131230 count as 2
20131227 count as 3
20131226 count as 4
20131225 count as 5
20131224 count as 6
20131223 count as 7
20131220 count as 8
20131219 count as 9

excluded 20131229, 20131228, 20131222, 20131221 since they are sat and sundays.

If current day changes last month end need to be checked whether it is falling in between 0 to 9 dates.

I hope have expalined.

Please Advise, how to achieve and if possible if known let me know how to use Mr.Perderabo's Date Calculator for my requirement.

Thanks,
Regard,
karthik
# 4  
Old 12-31-2013
you want to run through a count starting from 0-9 (10), stepping back each iteration as a day in time. Then you want to identify any day that might be a Saturday or Sunday? I think this will work?



Code:
counter=0

while [[ $counter -le 9 ]]
 do
   case $counter in
        [0-9])
          LDATE=`date -d "$counter days ago" +%Y%m%d`
          DOW=`date -d $LDATE +%u`

          case $DOW in
             7)
                DAY="Sunday";;
             6)
                DAY="Saturday";;
             *)
                DAY="Mon - Fri";;
          esac;;
   esac

   echo "$LDATE - $DAY - $counter"

   (( counter += 1 ))
 done

This User Gave Thanks to Blackacid For This Post:
# 5  
Old 12-31-2013
Would this help you ?

We can use following option available in date

%u day of week (1..7); 1 is Monday

%w day of week (0..6); 0 is Sunday
Code:
#!/bin/bash

today=$(date +"%Y%m%d")
last_consecutive="10"

i=1;k=1
while :; do

    date=$(date -d"$today - $i day" +"%u %Y%m%d")
    [ "${date:0:1}" -lt "6" ] && echo "${date:2}" "$k" && k=$((k+1))        
    [ "$k" -eq "$last_consecutive" ] && break || i=$((i+1))
    
done

Code:
$ bash last_10.sh
20131231 1
20131230 2
20131227 3
20131226 4
20131225 5
20131224 6
20131223 7
20131220 8
20131219 9

This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 01-01-2014
Hi Balckacid/Akshay/All Unix Techies,

Thanks for looking into my requirement and both the scripts works fine in ksh and it is good.

Now i can compare the last end of month date whether it is falling in last 10 days.

Thanks,
Regards,
karthik
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all CONSECUTIVE text lines from file shell scripting

Hi I have a text file like below. THe content of the text will vary. Entire text file have four consecutive lines followed with blank line. I want to delete the occurrence of the two consicutive lines in the text file. I don't have pattern to match and delete. Just i need to delete all... (5 Replies)
Discussion started by: RJSKR28
5 Replies

2. Shell Programming and Scripting

How to obtain a day of the week from the date?

I have a date in format YYYYMMDD, i need to get the day of the week from the given date. I am working in AIX system. ---------- Post updated at 09:59 AM ---------- Previous update was at 09:57 AM ---------- Tried to post sum of the thread's link from which i tried, but de rules didnt allow me... (9 Replies)
Discussion started by: baranisachin
9 Replies

3. Shell Programming and Scripting

How to add day of week at the end of each line that shows the date?

I have a file that looks like: file1: www_blank_com 20121008153552 www_blank_com 20121008162542 www_blank_com 20121009040540 www_blank_com 20121009041542 www_blank_com 20121010113548 www_blank_com 20121011113551 www_blank_com 20121012113542 I want the new file to show the day of... (3 Replies)
Discussion started by: castrojc
3 Replies

4. Shell Programming and Scripting

How to add trailer record at the end of the flat file in the unix ksh shell scripting?

Hi, How to add trailer record at the end of the flat file in the unix ksh shell scripting can you please let me know the procedure Regards Srikanth (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

5. Shell Programming and Scripting

Finding Day of the week from date

I have a problem of Finding Day of the week from date, but i need to do it within awk On SOLARIS Input:20101007(YYYYMMDD) Output:Thursday kindly provide suggestions. Thanks in advance (8 Replies)
Discussion started by: junaid.nehvi
8 Replies

6. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

7. Shell Programming and Scripting

Function to get day of week from YYYY-MM-DD date

Can't find out how to get the day of the week from a given date, anyone got a code snippet that could help please? Ta!! (4 Replies)
Discussion started by: couponmeup
4 Replies

8. Shell Programming and Scripting

Display the day of the given date-ksh

Hi friends, I need some urgent help on Dates in unix. I have a date say - "20080706", i need some command to display the day, here its sunday(06). Please help me out. FYI: I use Ksh. I/P - 20080706 O/P - Sunday (1 Reply)
Discussion started by: divzz
1 Replies

9. HP-UX

Get Day of Week from date

Hi All, I have date in string format 'YYYY-MM-DD'. I want to know day of the week for this date. Example. For '2005-08-21' my script should return '0' or Sunday For '2005-08-22' it should return '1' or Monday I want piece of code for HP-UX korn shell. Appreciate reply on this. (5 Replies)
Discussion started by: vpapaiya
5 Replies

10. UNIX for Dummies Questions & Answers

How to find Day of the Week from the given date (Perl)?

How to find the Day of the Week of the given Date using perl? If I have a date in YYY--MM-DD format, how to find the DOW? Based on that, I need to find the following sunday. Pls help. (5 Replies)
Discussion started by: deepakwins
5 Replies
Login or Register to Ask a Question