Getting week number of the "Monday"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting week number of the "Monday"
# 1  
Old 11-06-2006
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. Smilie
S

Last edited by sbasetty; 11-07-2006 at 09:18 PM..
# 2  
Old 11-06-2006
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.
# 3  
Old 11-07-2006
Question is not clear actually. Can you please refine your question please? Smilie
# 4  
Old 11-07-2006
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.
# 5  
Old 11-07-2006
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 Smilie

Last edited by sbasetty; 11-07-2006 at 09:20 PM..
# 6  
Old 11-08-2006
/^[[: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}]"

# 7  
Old 11-08-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Solaris

The slices "usr", "opt", "tmp" disappeared!!! Help please.

The system don't boot. on the screen appears following: press enter to maintenance (or type CTRL-D to continue)...I checked with format command. ... the slices "0-root","1-swap","2-backup" exist. ...the slises "3-var","6-usr" -unassigned. :( (16 Replies)
Discussion started by: wolfgang
16 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. HP-UX

[Solved] crontab issue "day of week"

Is there an issue with running a cron entry like the below entries? 0 2 21 12 2 cd /usr/local/bin;./cksecurity.sh -f /home/theninja/security.dat21 I wanted this to run on Tues at 2am, which it did successfully, however I also had the following entry to run next tuesday that also ran on 12/21.... (3 Replies)
Discussion started by: theninja
3 Replies

7. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question