|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 08:18 PM.. |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
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
|
|||
|
|||
|
Question is not clear actually. Can you please refine your question please?
![]() |
|
#4
|
||||
|
||||
|
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
|
|||
|
|||
|
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 08:20 PM.. |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
/^[[: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 30There 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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 08:59 AM |
| [Solved] crontab issue "day of week" | theninja | HP-UX | 3 | 12-21-2010 02:09 PM |
|
|