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. :slight_smile:
S

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.

Question is not clear actually. Can you please refine your question please? :confused:

Try and adapt the following script (based on cal command output) :

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:

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

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 :slight_smile:

/^[[:digit:][:space:]]*$/ Select lines containing digits and spaces only.

       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.

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}]"

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.

/^[[:digit:][:space:]]*$/ && NF>0 Select non empty lines (NF>0) containing digits and spaces only.

if (substr($0, 1, 2) != " ") wm += 1; Count weeks in the month (lines starting whith a number in cols 1-2 (Sun))

       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

Jean-Pierre.

Thank you very much Jean
It was very helpful

Thanks :slight_smile:
S