Hi,
Please can someone help me in getting first sunday date of a month.
i_year=`date +%Y`
ny_first_sun_nov=`cal 10 $i_year | sed '/^$/d' |head -3 |tail -1| rev | cut -c1`
This works good if the first sunday has a value but not if it is blank and first sunday falls on second week.
cal -m where I can make Monday as first day of the week so that Sunday always have a value does not work on Solaris.
Please can someone help me to get this corrected?
Thanks
Yoda
May 8, 2013, 11:33am
2
I noticed that you opened several threads lately regarding date arithmetic and you didn't apply any suggestions posted in your previous threads!
Before going forward I suggest you to refer this thread in FAQ section.
Thanks Yoda,
I will close the other threads with suggestions and comments,
All the threads correspond to different questions/ arithmetic but I should close them if they are resolved.
Thanks again
clx
May 8, 2013, 11:51am
4
Try ..
cal 12 2012 | awk 'NF==7 && !/^Su/{print $1;exit}'
1 Like
Its not working,
cal 12 2012 | awk 'NF==7 && !/^Su/{print $1;exit}'
is giving me S
I am looking for value that is 1
clx
May 8, 2013, 12:16pm
6
That's working for me.
$ cal 1 2012 | awk 'NF==7 && !/^Su/{print $1;exit}'
1
$
$ cal 1 2012
January 2012
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 31
Please use /usr/xpg4/bin/awk
on solaris
Also, post the output of
cal 1 2012
cal 1 2012
January 2012
S M Tu W Th F S
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 1 2012 | awk 'NF==7 && !/^Su/{print $1;exit}'
S
---------- Post updated at 10:00 PM ---------- Previous update was at 09:58 PM ----------
/usr/xpg4/bin/awk
this is also giving me S
infyanurag:
cal 1 2012
January 2012
S M Tu W Th F S
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 1 2012 | awk 'NF==7 && !/^Su/{print $1;exit}'
S
---------- Post updated at 10:00 PM ---------- Previous update was at 09:58 PM ----------
/usr/xpg4/bin/awk
this is also giving me S
As you can see, the output of "cal 1 2012" in your system is not the same as in that of clx.
He is using "Su" for "Sunday" - that's so he could avoid parsing the line that starts with "Su".
But you have "S" for "Sunday", so now you know the change you have to make in your script to make it work.
1 Like
Yoda
May 8, 2013, 12:37pm
9
cal 5 2013 | awk 'NF==7&&NR>2{print $1;exit}'
1 Like
Thanks Yoda, it worked, can we also use it to get the second sunday?
I mean to get it generalized?
Yoda
May 8, 2013, 1:25pm
11
Here is a generalized approach for sunday:
cal 5 2013 | /usr/xpg4/bin/awk -v n=2 'NR>2&&!/^ /{if(++c==n) {print $1;exit}}'
Change n
variable value as per your requirement.
1 Like
Many Thanks to all, especially Yoda. Now I have learnt how to do this by your help
The previous expressions suffer from the format; the first week has different number of fields depending on the month.
The following tries to correct that
cal 5 2013 |
sed 's/\(..\) /\1|/g' |
awk -F"|" '(NF==7 || $1~/[0-9]/) && $day~/[0-9]/ && ++c==count {print $day+0}' day=1 count=2
You can put various days and counts ...
Jotne
May 8, 2013, 3:03pm
14
Same as I posted here:
Hi, I want to get the second Sunday of march in any year, I have tried below command but it is not giving me the correct output pre { overflow:scroll; margin:2px; padding:15px; border:3px inset; margi | The UNIX and Linux Forums
How many threads are needed to solve one problem?
Yoda
May 8, 2013, 3:17pm
15
Jotne, please calm down. I noticed before that OP opened several similar threads, hence I requested him/her to refer another thread for ideas.
You can check my previous post.
But OP had some trouble with the way he/she getting cal
command output, so he/she requested for additional assistance with tweaking the solutions posted.
That's all.
1 Like
Jotne
May 8, 2013, 3:26pm
16
I am very calm
Found this
https://bugs.launchpad.net/ubuntu/\+source/bsdmainutils/\+bug/908233
Other solution
ncal -m 5 | awk '/Su/ {print $3}'
12
1 Like
OMG 11 threads! ELEVEN THREADS!!
Time! Calendars! Didn't anyone pay attention in grade school? If day 1 of the month is not Sunday (0) according to the dow from GNU date '+%D', then it is 7-dow days away on the date ( 1 + 7 - dow )? The general solution is (( 7 - dow(1st) ) % 7 ) + 1. Ooh, the numbers go spinning around so!