Extract from cal

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract from cal
# 1  
Old 08-11-2014
Linux Extract from cal

I was trying to get 1st Sunday in a month. I tried using cal [month] [year] followed by awk NF=1 apparently it would give entire 1st field in that month.

Any suggestions
# 2  
Old 08-11-2014
This isn't pretty, but I think it works...
cal 11 2014 | cut -c1-3 | grep "[0-9]" | head -1
foo=`cal 11 2014 | cut -c1-3 | grep "[0-9]" | head -1`
echo ${foo}

# 3  
Old 08-11-2014
Yup it worked. Thanks
# 4  
Old 08-11-2014
cal 12 2014 | awk 'NR==4{print $1}'

# 5  
Old 08-11-2014
Originally Posted by protocomm
cal 12 2014 | awk 'NR==4{print $1}'

That will get the first or second sunday depending on the year. It works for 2014.
# 6  
Old 08-11-2014
So, let's look at what we get from cal (either with no operands, or a month operand and a year operand):
    August 2014
Su Mo Tu We Th Fr Sa
                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

From this we can see that the 1st number on a line with 7 fields (after the header line showing the days of the week) is the 1st Sunday. So, a simple awk shell script for this is:
cal $1 $2|awk 'NF == 7 && NR > 2 {print $1; exit}'

If you invoke it with no operands, it will give you the 1st Sunday of the current month:


If you invoke it with a month and year as operands, it will give you the 1st Sunday of that month in that year:
./sun1 6 2014


Many of the other scripts suggested would print 8 for this instead of 1.

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xxpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 08-11-2014
Excuse me, i don't understand completely the post.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cal -m on bash

Hi, I want to make Monday as the first day of the month while using cal command when I execute without bash, its working fine /bin/sh cal -m 03 2013 March 2013 Mo Tu We Th Fr Sa Su 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... (5 Replies)
Discussion started by: infyanurag
5 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

another cal command question.

I got this from this board yesterday cal | xargs -n1 | tail -1 which displays the current months days.. for instance if you type this in a shell today you will get 31. I would like to also display the month and year.. something like March 2011 has 31 days. how would I do that? ... (3 Replies)
Discussion started by: rontopia
3 Replies

4. 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

5. Shell Programming and Scripting

Get day of week from cal

Hi all, I am trying to get dow from cal using below script #! /bin/bash YEAR=`echo $1 | cut -c 1-4` MONTH=`echo $1 | cut -c 5-6` DAY=`echo $1 | cut -c 7-8` for i in 1 2 3 4 5 6 7 do dayofweek=`cal $MONTH $YEAR | awk '$i == $DAY {printf("%s","$i")}'` echo $dayofweek... (4 Replies)
Discussion started by: bzylg
4 Replies

6. Shell Programming and Scripting

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

doubt in cal command

I am new to unix... How to get all the saturdays of a specific year? for a specific month, i tried as below.. cal 02 2006 | awk '{print $7}' but it is not giving all saturdays.... can anyone help me with this? Thanks in advance, Sumi (9 Replies)
Discussion started by: sumi
9 Replies

9. UNIX for Dummies Questions & Answers


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

Featured Tech Videos