parsing cal cmd


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing cal cmd
# 1  
Old 11-14-2008
parsing cal cmd

Smilie 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
# 2  
Old 11-14-2008
Using the gnu date command is far easier than cal. Do you have gnu date? (Are you on linux? answers the same question).
# 3  
Old 11-17-2008
Question need help parsing return from cal command

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
# 4  
Old 11-18-2008
Code:
#!/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

# 5  
Old 11-18-2008
Or use awk:
Code:
cal 11 2008 | \
awk -v v=8 '
NR==2{
   for(i=1;i<=NF;i++) a[i]=$i
}
NR>2{
   for(i=1;i<=NF;i++)
        {
        if($i==v) d=a[i]
        }
}
END{
print d
}'

# 6  
Old 12-08-2008
Thanks everyone for all you help
# 7  
Old 12-30-2008
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract from cal

I was trying to get 1st Sunday in a month. I tried using cal followed by awk NF=1 apparently it would give entire 1st field in that month. Any suggestions (11 Replies)
Discussion started by: penqueen
11 Replies

2. Homework & Coursework Questions

Using cal in a script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a shell script that will: "Display" the number of days in the current month. For example: September... (1 Reply)
Discussion started by: eaafuddy
1 Replies

3. Shell Programming and Scripting

Perl open(CMD, "cmd |"); buffering problem..

Hello, There's a third-party application's command that shows the application's status like "tail -f verybusy.log". When use the command, the output comes every 1-sec. but when it goes in a script below the output comes every 8-sec...What is the problem and how can I fix it? open(CMD,... (2 Replies)
Discussion started by: Shawn, Lee
2 Replies

4. Shell Programming and Scripting

Unix cmd prompt how to get old cmd run?

Hi, I am using SunOS I want to serch my previous command from unix prompt (like on AIX we can search by ESC -k) how to get in SunOs urgent help require. (10 Replies)
Discussion started by: RahulJoshi
10 Replies

5. UNIX for Dummies Questions & Answers

cal command

Hello, I wanted to display calender for the previou, current and next month in a single command... I used the command cal -3 for this. But its throwing me a Bad Argument error. I am using HP UX to execute this command. Is this a syntax error, or let me know if there any other ways to... (6 Replies)
Discussion started by: atlantis
6 Replies

6. Shell Programming and Scripting

parsing return from cal command

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... (1 Reply)
Discussion started by: dragrid
1 Replies

7. UNIX for Dummies Questions & Answers

Cal question

This probably would be a cake walk for you, but i am having trouble with this. I am trying to print every tuesday of the month from cal, and the FS default is space. There is one row that has few spaces at the beginning and so when i print $3, those spaces get ingnored and a different day gets... (2 Replies)
Discussion started by: Vin
2 Replies

8. UNIX for Dummies Questions & Answers

man <cmd> >> cmd.txt

I've noticed most of my postings here are because of syntax errors. So I want to begin compiling a large txt file that contains all the "man <cmd>" of the commands I most have problems with. I ran a "man nawk >> nawk.txt" but it included a header/footer on each "page". Anyone know how I'd be... (6 Replies)
Discussion started by: yongho
6 Replies

9. UNIX for Dummies Questions & Answers

cal

hey everyone. I'm new to UNIX, and I'm having trouble with the cal command. I know that you can display a calendar if you just type in 'cal 3 2005' for example. But how would you do it if you just wanted the calendars displayed to be from March 2005 to June 2005? Thanks (4 Replies)
Discussion started by: pythonman
4 Replies

10. UNIX for Dummies Questions & Answers

Cal command

I am trying to configure the cal command to recognize the month names. When you type: cal - you get the calander for the current month of the current year. Is there a way of making the system recognize March, and Mar. So I could type: cal March or cal mar and get the same response as cal.... (5 Replies)
Discussion started by: Astudent
5 Replies
Login or Register to Ask a Question