parsing cal cmd

:o given a date example mm dd yyyy 01 02 1999
how can your parse cal 01 1999 and find the date in the above case 02 and display what the actual day was eg s m t w t f s

Thanks in advance Dragrid

Using the gnu date command is far easier than cal. Do you have gnu date? (Are you on linux? answers the same question).

Jim , Anyone
I do not have GNU date
Besides I am particularly interested in how one can parse the return from the cal command. Say do - cal 11 2008 - and parse out a given date, say the 8th and return that the 8th was Saturday. ( diffrentiating between S for Saturday and Sunday , also in the case of T between Tuesday and Thursday ) . I am trying to use awk but getting stuck. Also can this be done using ARRAY

#!/bin/ksh
set -A daynames Sun Mon Tues Wed Thur Fri Sat

dow() # convert date to dow
{
  # $1 month  $2 day $3 year 
    
  cal $1 $3 | sed '/^$/d' |\
  awk 'BEGIN {getline; getline}
      { if(days==0) {offset= 7 - NF} for(i=0; i<NF; i++) {days++}
      } END { print offset, days }' | read offset days
  typeset -i day=$2
  if [[ $day -le $days && $day -gt 0 ]]  
  then
  	 echo ${daynames[ $(( (($day + offset) % 7) -1 )) ]}
  else 
     echo "Invalid parameter" 
  fi
}

dow 11 02 2008 
dow 01 01 1999

Or use awk:

cal 11 2008 | \
awk -v v=8 '
NR==2{
   for(i=1;i<=NF;i++) a=$i
}
NR>2{
   for(i=1;i<=NF;i++)
        {
        if($i==v) d=a
        }
}
END{
print d
}'

Thanks everyone for all you help

Hi Jim

Trying you code , having a little problem getting it to work -
do you feed the script the args mm dd yyyy

Please let me know how to execute

Thanks...

Hi Dan,

you code is interesting - I am not sure if it addressess my question because i am looking for the actual day for the date and I do not see where or how that date arg is fed to the script ....example the script should be that say you feed 12 03 2008 to the script it will return "Wednesday" ....... Thanks