how can i find the third friday of each month?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how can i find the third friday of each month?
# 1  
Old 05-30-2007
Bug how can i find the third friday of each month?

Help please! I need to read the calendar and put the date of the third Friday of each month into a variable for comparison in an "if" statement. How would I do this?

Thnx,
leslie02
# 2  
Old 05-30-2007
The third friday is always between days 15 and 21 inclusive of the month .
You can made the test like that :
Code:
day_of_week=$(date +%w)  # (0..6); 0 is Sunday, 5 is Friday
day_of_month=$(date +%e) # space padded
if [ ${day_of_week} -eq 5 -a ${day_of_month} -ge 15 -a ${day_of_month} -le 21 ]
then
   echo "third Friday "
fi

Jean-Pierre.
# 3  
Old 05-30-2007
Leslie,
See if this works for you:
Code:
typeset -i mMth=1
mYear='2007'
while [ ${mMth} -le 12 ]
do
  m3Friday=`cal ${mMth} ${mYear} | tail +3 | cut -c16,17 | sed '/^ *$/d' | sed -n '3p'`
  echo "Third Friday of "${mMth}"/"${mYear}" = "${m3Friday}
  mMth=${mMth}+1
done

# 4  
Old 05-31-2007
This should work

Thanks
Ashok

cal 07 2007 | cut -c16-18 | tail -4 | head -1
# 5  
Old 05-31-2007
Code:
cal 05 2007 | sed -n "5{s/ *[0-9]\{2\}$//;s/^.* //p;}"

# 6  
Old 05-31-2007
Shell_Life, akrathi, anbu23, it seems that your solutions do not work in all the cases :
Code:
$ cal 12 2007
       December 2007      
Sun Mon Tue Wed Thu Fri Sat  
                         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  31

$ cal 12 2007 | tail +3 | cut -c16,17 | sed '/^ *$/d' | sed -n '3p'
 2
$ cal 12 2007 | cut -c16-18 | tail -4 | head -1
 13
$ cal 12 2007 | sed -n "5{s/ *[0-9]\{2\}$//;s/^.* //p;}"
14
$

A possible solution :
Code:
$ cal 12 2007 | awk 'NR>2 && NF>=2 && ++w==3 {print $6}'
21
$ cal 07 2007 | awk 'NR>2 && NF>=2 && ++w==3 {print $6}'
20
$ cal 6 02007 | awk 'NR>2 && NF>=2 && ++w==3 {print $6}' 
15
$

Jean-Pierre.
# 7  
Old 05-31-2007
this one doesn't use cal
Code:
count=0
month="05"
for s in `seq 1 31`
do
 fr=$(date +%a --date="2007-$month-$s" )
 if [ "$fr" == "Fri" ];then
     friday[$count]=$s
     count=$((count+1))
 fi 
done
echo ${friday[2]} #third fri

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Bash Display First Friday of the next month

Hello, I need to find the date of next first Friday of the month and set as a variable in a bash script ie - FIRSTFRIDAY=$(date -dfirst-friday +%d) I know date -dfirst-friday doesn't work, but unsure if I can use this / cal + awk or something else to find the right date of the... (7 Replies)
Discussion started by: summerdays
7 Replies

2. Shell Programming and Scripting

Find one month before date

Hi, I want two dates one will be the current date and the other one will be just one month before. Say if current month is 11/4/2014 then the other date should be 11/3/2014. #!/bin/ksh currentDtae=`date` oneMonthBefore= ? I dont know how to do it. Went through some of the related threads... (15 Replies)
Discussion started by: Sharma331
15 Replies

3. UNIX for Advanced & Expert Users

How to find last two files for a month?

Hi All, I need to find last two files for the month. lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 15:43 c.txt -rwxr-xr-x 1 user userg 1596 Mar 21 15:43 d.txt -rwxr-xr-x 1 user userg 1596 Mar 22 15:43 f.txt -rwxr-xr-x 1... (14 Replies)
Discussion started by: Makarand Dodmis
14 Replies

4. Red Hat

How to find/display out last Friday's date of the month?

Hello, Can you please help me find/display out last Friday's date of the month using command in Unix/Linux (3 Replies)
Discussion started by: sunnysthakur
3 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Cron - job to run every 3rd Friday of the month only

Hi Expert Please help me to set a cron job schedule, Ihave a job that run every 3rd Friday of the month at 1030am. I tried to set up like this, but the job still runs every friday at 1030am. I want the job to run every 3rd Friday of the month at 1030am only 30 10 15,16,17,18,19,20,21... (2 Replies)
Discussion started by: kaibiganmi
2 Replies

6. Shell Programming and Scripting

cron job to run on second to last friday each month

I needed a cron job to run on the second to last friday of every month. Our servers are running HP-UX, and the HP-UX date command is pretty basic and does not have all of the fancy options that Linux date command does, and it does not have the ability at all to return future dates. So I had to... (0 Replies)
Discussion started by: lupin..the..3rd
0 Replies

7. Shell Programming and Scripting

Last friday of every month

Hi, I need to get the date of last friday of every month. how can i achieve this ? please guide me. Thanks in advance (3 Replies)
Discussion started by: apsprabhu
3 Replies

8. UNIX for Dummies Questions & Answers

find out month from a date

I would like to find out the month from a given date, how is it possible. (5 Replies)
Discussion started by: rudoraj
5 Replies

9. Shell Programming and Scripting

Need help, Every friday in a month

I am trying to write a script that shows every Friday in a month. I used cal $1 $2 | grep -v "^$" | awk '{print $6}' It doesn't work for the frist week of Friday because calendar command output has some spaces in the first line and awk '{print $6}' doesn't work. Anybody help me with this... (3 Replies)
Discussion started by: LAY
3 Replies

10. Shell Programming and Scripting

Find all files by month

Hi, I am trying to do achieving of files by months. find /test -name \*.* -mtime +30 will give me the result of all modified files after 30 days. But lets say i want to list all files that is modified in last months... what is the command to do it? Thanks! (13 Replies)
Discussion started by: maldini
13 Replies
Login or Register to Ask a Question