I wanted to get the date of the first monday of a month.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers I wanted to get the date of the first monday of a month.
# 1  
Old 09-07-2007
I wanted to get the date of the first monday of a month.

Hi,

I need to display the date of the first monday of a month.

Can any one please help me on this.

Thanks in advance.
# 2  
Old 09-07-2007
Hi.

The use of Linux "date" helps for this task. However, it's not portable. I could not think of a clever method of doing this, so here's a way that uses awk and cal, both of which should be easily found on all *nix boxes:
Code:
#!/bin/sh -

# @(#) s2       Demonstrate extraction of date of first Monday of
# month and year from display of "cal" utility..

# Usage: s2 [month -> this month] [year -> this year]

debug=":"
debug="echo"

CAL=/usr/bin/cal

todaymonth=$( date +%m )
todayyear=$( date +%Y )
MON=${1-$todaymonth}
YEA=${2-$todayyear}

$CAL $MON $YEA |
awk '
NR == 1 { next }
NR == 2 { next }
NF <= 5 { next }
NF == 6 { print $1 ; exit }
NF == 7 { print $2 ; exit }
'

exit 0

which produces:
Code:
% ./s2
3

and:
Code:
% ./s2 10 2008
6

You can make the awk shorter, but I did this way so that it would be easier to read. See man pages for details ... cheers, drl
# 3  
Old 09-07-2007
Code:
cal | sed '1d' | cut -d" " -f 2 | head -3 | sed '/^$/ d' | grep -iv mon

# 4  
Old 09-08-2007
Hi.

The pipeline solution:
Code:
cal | sed '1d' | cut -d" " -f 2 | head -3 | sed '/^$/ d' | grep -iv mon

from bhargav produces:
Code:
Mo
2

for me. Yet for Spetember 2007:
Code:
   September 2007
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

The problem probably lies in the cut step, where the leading blank in the rows beginning with " 2" and " 9" is counted as a column ... cheers, drl
# 5  
Old 09-08-2007
Code:
number=$(date +"%u%d")
if [ $number -le 107 ] ;then
.....
fi

# 6  
Old 09-12-2007
Hi,

Thank you for your help.
I am very new to unix.
can you please explain me the below code and the logic you had used.


$CAL $MON $YEA |
awk '
NR == 1 { next }
NR == 2 { next }
NF <= 5 { next }
NF == 6 { print $1 ; exit }
NF == 7 { print $2 ; exit }
'
# 7  
Old 09-15-2007
Hi, Sheethal.

Let us begin with:
Code:
$CAL $MON $YEA |

the shell will substitute defined values for $string, so that in the default case for today, this would become:
Code:
/usr/bin/cal 09 2007

I suggest you run cal a few times to see how it works, especially with different arguments. Look at the output closely to see how many "fields" there are on each line of output in the calendar.

Then the shell will connect the output of cal to the next command because of the trailing "|".

We can discuss the awk script after you're comfortable with cal ... cheers, drl

PS. Apparently the email notifications are currently disabled at unix.com due to server issues, so there may be delays in communication.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to find second and fourth Monday of the month?

Hi, I have came across the scenario where, we have to run the script on second and fourth Monday of each month. I have tried to search man page of date and also forum for it but, could not get any answer to this. Can you please advise how can we get second and fourth Monday of the month? ... (18 Replies)
Discussion started by: Prathmesh
18 Replies

2. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

3. Shell Programming and Scripting

Get Date of Previous and next Monday

Hi, I would like to know how to get Previous Monday and Next Monday from the current date.. thanks (5 Replies)
Discussion started by: balasubramani04
5 Replies

4. UNIX for Dummies Questions & Answers

Can we get every tuesday or monday's date for the current week

Hi Can we get every tuesday or monday's date for the current week ? For the current week i need tuesday's date or monday's date in %m%d%y fromat Thanks (5 Replies)
Discussion started by: laxmi131
5 Replies

5. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

6. Shell Programming and Scripting

Get the date of monday when running from Friday to next Thursday

Hi, I have a requirement where I want to get the date of monday when I am running the script from previous Friday to the following Thursday. For example: When ever I run the script between 19thFeb2010(Friday) to 25th Feb 2010(Thursday), I should get the date of 22nd Feb 2010 in the format of... (5 Replies)
Discussion started by: fasiazhar_411
5 Replies

7. UNIX for Advanced & Expert Users

Crontab For First Monday Of Every Month!!

Hi, Could any one please let me know the crontab entry for scheduling a job for every first monday of the month? Thank You in advance, Sue (2 Replies)
Discussion started by: pyaranoid
2 Replies

8. UNIX for Dummies Questions & Answers

cron script -run every 2nd day of month except Monday

I know I can't schedule this in cron and would have to write a wrapper around my script and schedule it in cron ....but not sure how do to this? How do I exclude Monday if the 2nd day of the month falls on a Monday? Thanks. I tried this: 0 0 2 * 0,2-6 command And I know this doesnt... (2 Replies)
Discussion started by: newtou
2 Replies

9. UNIX for Advanced & Expert Users

Extract Monday from given date

Hi I want to extract the date on Monday depending upon the user input for that week. For example if the input date is 20080528 then the output should be 20080526. If the input is 20080525 then it will be 20080519 i am working on IBM AIX Thanks (2 Replies)
Discussion started by: itsme_maverick
2 Replies

10. UNIX for Dummies Questions & Answers

Crontab First Monday of Month only

Is there a way to setup a cronjob that will only run on the first monday of the month? (3 Replies)
Discussion started by: molonede
3 Replies
Login or Register to Ask a Question