Getting week number of the "Monday" | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Getting week number of the "Monday"

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-06-2006
sbasetty sbasetty is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 27 October 2012, 12:16 PM EDT
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Getting week number of the "Monday"

Hi Friends,

Can you help me with this,
I would like to get the week number of the "Monday",
Say if we run on first week of november it should give me output as "05" and "10" i.e it says the monday falls on 5th week of october.
If we run on the second week of november it should give me "01" and "11".
Says Monday falls on 1st week of november.

Any help is highly appreciated.

Thanks in advence.
S

Last edited by sbasetty; 11-07-2006 at 09:18 PM..
Sponsored Links
    #2  
Old 11-06-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 24 November 2014, 10:48 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 438 Times in 261 Posts
I don't really follow you here. But read the date arithmetic article in our FAQ section and look at datecalc, which can probably do whatever it is that you want to do.

datecalc -c 2006 10 1

will return an integer from 0 to 6 to indicate the day of the week for Oct 1, 2006. Once you have the DOW of the first day of a month, subtract that from 9 which will give the the day of the first or second Monday in the month. So if that is larger than 7, subtract 7 from it. Now you are guaranteed to have the day of the month which is the first Monday.
Sponsored Links
    #3  
Old 11-07-2006
SharmaPCS SharmaPCS is offline
Registered User
 
Join Date: Aug 2006
Last Activity: 18 July 2007, 7:38 AM EDT
Location: Hyderabad
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Question is not clear actually. Can you please refine your question please?
    #4  
Old 11-07-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 27 October 2014, 1:32 PM EDT
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 61 Times in 57 Posts
Try and adapt the following script (based on cal command output) :

Code:
day=${1:-$(date +%e)}
month=${2:-$(date +%m)}
year=${3:-$(date +%Y)}

#
# Determine week in the month
#

wmonth=${month}
wyear=${year}

cal ${wmonth} ${wyear} | \
awk -v day="$day" '
        /^[[:digit:][:space:]]*$/ && NF>0 {
           if (substr($0, 1, 2) != "  ") wm += 1;
              for (i=1; i<=NF; i++) {
                if ($i+0 == day+0) {
                    print wm;
                    exit;
                 }
              }
           }
    ' | read week

#
# Determine week in previous month
#

if [ -z "${week}" ]
then

   # Previous month/year

   (( wmonth = month - 1 ))
   if (( wmonth == 0 ))
   then
      wmonth=12
      (( wyear -= 1))
   fi

   # Determine week

   cal ${wmonth} ${wyear} | \
   awk '
        /^[[:digit:][:space:]]*$/ && NF>0 {
           if (substr($0, 1, 2) != "  ") wm += 1;
        } 
        END { print wm }
    ' | read week
fi

#
# Display result
#

echo "Date = ${day}-${month}-${year}, Week.Month.Year = ${week}.${wmonth}.${wyear}"

Examples:
Code:
$ wm.ksh
Date =  7-11-2006, Week.Month.Year = 1.11.2006

$ wm.ksh 1 11
Date = 1-11-2006, Week.Month.Year = 5.10.2006


Jean-Pierre.
Sponsored Links
    #5  
Old 11-07-2006
sbasetty sbasetty is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 27 October 2012, 12:16 PM EDT
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Thank Jean,


For calculating the previous month/year week the code is giving error.
when I run it displays week number as null for input value of "1 11"

I have replaced it with nawk now it displays as "0".

What will the /^[[:digit:][:space:]]*$/ statement will do
can you explain .
Can you please helpme with this.

With "nawk"

myWeek=$(cal "{wmonth}" "${wyear}" | \
nawk '
/^[[:digit:][:space:]]*$/ && NF>0 {
if (substr($0, 1, 2) != " ") wm += 1;
}
END { printf("%d\n", wm) }
')
echo "myWeek number is -> [${myWeek}]"


Thanks a lot

Last edited by sbasetty; 11-07-2006 at 09:20 PM..
Sponsored Links
    #6  
Old 11-08-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 27 October 2014, 1:32 PM EDT
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 61 Times in 57 Posts
/^[[:digit:][:space:]]*$/ Select lines containing digits and spaces only.

Code:
       November 2006      
Sun Mon Tue Wed Thu Fri Sat  
             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

There is a little error in your code, a $ is missing.

Code:
myWeek=$(cal "${wmonth}" "${wyear}" | \
         nawk '
               /^[[:digit:][:space:]]*$/ && NF>0 {
                  if (substr($0, 1, 2) != " ") wm += 1;
               }
               END { printf("%d\n", wm) }
              ')
echo "myWeek number is -> [${myWeek}]"

Sponsored Links
    #7  
Old 11-08-2006
sbasetty sbasetty is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 27 October 2012, 12:16 PM EDT
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Aigels,

Can you please explaing this

/^[[:digit:][:space:]]*$/ && NF>0 {
if (substr($0, 1, 2) != " ") wm += 1;


You are selecting the lines with digits and space only and NF > 0 then you are substring that .

can you explain this.

Thanks
S.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 09:59 AM
[Solved] crontab issue "day of week" theninja HP-UX 3 12-21-2010 03:09 PM



All times are GMT -4. The time now is 04:07 AM.