An other awk problem

Hi all...

:confused: :confused: :confused:

I'm looking for a way to get the nth day of mth week of a month based on the "cal" command:
Meaning the following:
$ cal 11 2001|cat -n
1 November 2001
2 S M Tu W Th F S
3 1 2 3
4 4 5 6 7 8 9 10
5 11 12 13 14 15 16 17
6 18 19 20 21 22 23 24
7 25 26 27 28 29 30
8

What i want, is to say " I want the 3rd day of week 4 ( with the cat -n that would be 6).
I'm looking for a comand using the awk command

looking something like:
cal 11 2001|cat -n|grep "my_week( here is 6)"|awk 'my function of awk ', so the output would be:
tuesday 20th november...

Thanx for your help...

Have a nice week-end!

Jason

Try this...

#! /usr/bin/ksh

year=$1
month=$2
week=$3
day=$4

set -A days Sun Mon Tue Wed Thu Fri Sat
set -A months XXX Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
cal $month $year

#
# Get date of last day in 1st week of month
dldw1=$(cal $month $year | sed -n '3s/. //gp')

#
# calculate what day-of-month is day/week
if [[ $week = 1 ]] ; then
        ((dom=day))
else
        ((dom=day+dldw1+((week-2)*7)))
fi

#
#  Calculate day of week

((dow = (dom+dldw1)%7 ))

#
#  All done...print it out

echo ${months[month]} ${dom}, $year is on ${days[dow]} 

exit 0

Nice code, Perderabo.

Here is an awk solution:

#!/bin/sh

year=$1
month=$2
week=$3
day=$4

export month week day

cal $month $year | cat -n |
awk -v m=$month -v w=$week -v d=$day \
   'BEGIN {split("Jan Feb ... Nov Dec",mo)
           split("Sun Mon Tue Wed Thu Fri Sat",dow)}
    NF>1&&$1==w+2 {
       if (d>=NF)
           print "non-existant day of week"
        else
           print dow[d+8-NF], mo[m], $(d+1)}'

You need to expand the month array where I have "...". I am assuming that when week 1 starts on Thursday, then Thursday would be day 1.

Output format is: Tue Nov 20

added code tags for readability --oombera

Do you want to try my shorter pipe ?

[for ksh clones]

let line=week+2

cal 11 2001 | head -$line| tail -1| cut -f$day_of_week -d " "

Thanx for your help...

Is there a way using awk, that would give something like
cal $month $year|cat -n | awk '{print $(NF-$a_variable)}', where i could precise a_variable?