Script to find n.of weekdays and n.of weekends in a given date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to find n.of weekdays and n.of weekends in a given date
# 1  
Old 09-11-2016
Script to find n.of weekdays and n.of weekends in a given date

Hi All,

Could you please provide the shell script to find number of weekdays and
number of weekends for a given date for that month.

Monday to friday should be considered as weekdays and Saturday and Sunday should be considered as weekends.

Date should be passed as parameter.

For example
if we pass date as 2016-09-10 then
O/P weedays weekends
22 8

if we pass date as 2016-02-12 then
O/P weedays weekends
21 8

if we pass date as 2016-08-05 then
O/P weedays weekends
23 8

Please help me.

Thanks
# 2  
Old 09-11-2016
Help me out - how can a single date be a number of weekdays and/or weekends? And, what is "weekends" in your context - two days per weekend?

And, should you want just the number of respective days for that month, neglecting the day, none of your examples is correct.

So - what are you after?

Last edited by RudiC; 09-11-2016 at 09:49 AM..
# 3  
Old 09-11-2016
Hi,
We will be passing date but we have to find number of weekdays and
number of weekends in that month of the year.

Monday to Friday should be considered as weekdays and
Saturday and Sunday should be considered as weekends.

Please help me.

Thanks

---------- Post updated at 07:05 PM ---------- Previous update was at 06:20 PM ----------

Hi Rudic,

Please help me.

Thanks.
# 4  
Old 09-11-2016
Quote:
Originally Posted by ROCK_PLSQL
Hi,
We will be passing date but we have to find number of weekdays and
number of weekends in that month of the year.
Monday to Friday should be considered as weekdays and
Saturday and Sunday should be considered as weekends.
Please help me.
Thanks
---------- Post updated at 07:05 PM ---------- Previous update was at 06:20 PM ----------
Hi Rudic,
Please help me.
Thanks.
Hello ROCK_PLSQL,

Could you please try following and let me know if this helps you.
Code:
cat scrip.ksh 
VAL=$1
MONTH=`echo $1 | awk -F"-" '{print $2}'`
YEAR=`echo $1 | awk -F"-" '{print $3}'`
cal $MONTH $YEAR | awk 'NR>2 && NF{if($0 ~ /^[[:space:]]/){A[$NF]++;};if(($0 !~ /^[[:space:]]/) && NF<7){B[$1]++;};if(NF==7){B[$1]++;A[$NF]++};Q=$NF} END{print "Number of weekdays are:" Q-length(A)-length(B) ORS "Number of weekdays are:" length(A)+length(B);}'

Now when I run above script.kshwith an argument(as shown by you in very first post), following will be the output.
Code:
./scrip.ksh 12-2-15
Number of weekdays are:20
Number of weekdays are:8

./scrip.ksh 12-10-14
Number of weekdays are:23
Number of weekdays are:8

./scrip.ksh 12-01-22
Number of weekdays are:22
Number of weekdays are:9

So you could do cal month year for above example dates and cross check the output too.
Code:
cal 2 15
     February 15    
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 2

cal 10 14
     October 14     
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

cal 1 22
     January 22     
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

EDIT: Adding non-one liner form of solution as follows too.
Code:
cat script.ksh
VAL=$1
MONTH=`echo $1 | awk -F"-" '{print $2}'`
YEAR=`echo $1 | awk -F"-" '{print $3}'`
cal $MONTH $YEAR | awk 'NR>2 && NF{
					if($0 ~ /^[[:space:]]/){
								A[$NF]++;
							       };
					if(($0 !~ /^[[:space:]]/) && NF<7){
										B[$1]++;
								          };
					if(NF==7){
							B[$1]++;
							A[$NF]++
						 };
					Q=$NF
			          } 
		        END       {
					print "Number of weekdays are:" Q-length(A)-length(B) ORS "Number of weekdays are:" length(A)+length(B);
				  }
                       '

I hope this helps you.

NOTE: Above code is tested in BASH and GNU awk.

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-11-2016 at 11:14 AM.. Reason: Adding non-one liner form of solution as follows too successfully now.
# 5  
Old 09-11-2016
Hi Singh,

Thanks for your response.

The script is giving correct result.

Instead of printing this result, it should be assigned to variables since I have to pass this result to hive script.

Instead of
Code:
Number of weekdays are:20
Number of weekdays are:8

to should be assigned to variables
Code:
var120
var28

Please help me.

Thanks
# 6  
Old 09-11-2016
Hello ROCK_PLSQL,

Please use code tags in all your posts as per forum rules for Inputs/commands/codes which you are using into your post. Could you please try following and let me know if this helps you.
Code:
VAL=$1
MONTH=`echo $1 | awk -F"-" '{print $2}'`
YEAR=`echo $1 | awk -F"-" '{print $3}'`
var120=`cal $MONTH $YEAR | awk 'NR>2 && NF{
                                        if($0 ~ /^[[:space:]]/){
                                                                A[$NF]++;
                                                               };
                                        if(($0 !~ /^[[:space:]]/) && NF<7){
                                                                                B[$1]++;
                                                                          };
                                        if(NF==7){
                                                        B[$1]++;
                                                        A[$NF]++
                                                 };
                                        Q=$NF
                                  } 
                        END       {
                                        print Q-length(A)-length(B);
                                  }
                       '`

var28=`cal $MONTH $YEAR | awk 'NR>2 && NF{
                                        if($0 ~ /^[[:space:]]/){
                                                                A[$NF]++;
                                                               };
                                        if(($0 !~ /^[[:space:]]/) && NF<7){
                                                                                B[$1]++;
                                                                          };
                                        if(NF==7){
                                                        B[$1]++;
                                                        A[$NF]++
                                                 };
                                        Q=$NF
                                  } 
                        END       {
                                        print length(A)+length(B);
                                  }
                       '`
echo $var120
echo $var28

If these variabes needed to be used in any other codes, you could remove echos at last(which I have used to print their values).

Thanks,
R. Singh
# 7  
Old 09-11-2016
Thanks a lot for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script to find a date variable and increment it

Hi, I have parameter file wo_location.prm which has a date variable $last_upd_date= 02032016. I need to write a unix shell script to find that variable and increment it by 1 day. The path to the file is root/dir_lc/shared/param/wo_location.prm and the variable is $last_upd_date. Any... (2 Replies)
Discussion started by: isenhiem
2 Replies

2. Shell Programming and Scripting

How to find the date of previous day in shell script?

Hi Experts, i am using the below code get the date of previous day. #!/usr/bin/ksh datestamp=`date '+%Y%m%d'` yest=$((datestamp -1)) echo $yest When i execute the code i am getting output as: 20130715 What i am trying here is, based on the date passed i am fetching previus day's... (0 Replies)
Discussion started by: learner24
0 Replies

3. Shell Programming and Scripting

How to find the router reboot date using script?

Hai Iam having router output in a text file.from this data how to find out the router reboot date and time using script bgl-ras-bng-bge-09>show version | grep Time Router Up Time - 61 days, 21 hours 31 minutes 49 secs bgl-ras-bng-bge-09>show clock Thu Feb 14 10:16:14 2013 IST output... (6 Replies)
Discussion started by: surender reddy
6 Replies

4. Shell Programming and Scripting

bash script to find date based on search string for continuesly updating file

Hi All, I am very new to UNIX and I have tried this for a longtime now and unable to crack it.... There is a file that is continuously updating. I need to search for the string and find the date @ which it updated every day..... eg: String is "work started" The log entry is as below: ... (1 Reply)
Discussion started by: Nithz
1 Replies

5. Shell Programming and Scripting

How to script to find the newer date in a text file?

Hi, I have a text file, foo.txt, it looks something like below. In the file there is a line that gives the date in the form of: Mon Jun 15 11:09:31 2008. I need to find which date is the newest and then store certain details of that list data to another file. So, in this sample text file, I... (6 Replies)
Discussion started by: boolean2222
6 Replies

6. What is on Your Mind?

What do you do during weekends ? :)

Its been a while since we had any fun polls or interesting topics in this forum. So, here I start a thread rather calling it as a question - " What do you do during weekends ? :) " For many, on hearing the term - weekends should excite them :b: for ( few ) some - uhhh .. its just another day... (11 Replies)
Discussion started by: matrixmadhan
11 Replies

7. UNIX and Linux Applications

Code to find weekends in Sybase.

Hi All, I need a help here, actually i want SQl code which will determine all Saturdays and Sunday in given year (say 2009) and display it's dates. E.g :- 3 ---> Saturday 4 --> Sunday (for 2009) and so on. Thanks in Advance for help. Regards, Arvind. (0 Replies)
Discussion started by: arvindcgi
0 Replies

8. Shell Programming and Scripting

shell script to find latest date and time of the files

Hi everyone, Please help:) I have a list of 1000 different files which comes daily to the directory.Some of the files are not coming to the directory now. I need to write a shell script to find the latest date and time of the files they came to the directory. The files should be unique.... (1 Reply)
Discussion started by: karthicss
1 Replies

9. UNIX for Dummies Questions & Answers

shell script to find files by date and size

Hi, I have a directory PRIVATE in which I have several directories and each of these have several files. Therefore, I need to find those files by size and date to back up those files in another directory. I don't know how to implement this shell script using ''find''. appreciate any... (1 Reply)
Discussion started by: dadadc
1 Replies

10. Shell Programming and Scripting

Script to find files on a given input date

Hello gurus, I need to write a script to find out all the file that got changed on a specific folder since a given input date (Date to be given as Input) Thanx (1 Reply)
Discussion started by: ar.karan
1 Replies
Login or Register to Ask a Question