|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Awk/sed to play on calender
Hello Awk'inas/Sed'ers; This is keep ringing on my mind for a while, onto play in calender with awk or sed . Given a date, month and Year would like to find out the day corresponding to it. Am still a noob on awk and sed, hence would like to learn it from your responses.Here it is; Code:
cal 10 1987
October 1987
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 31When it is 1, I would like to see the output as Thu. Similarly for other dates would like to see the matching days of it. PS: One liner will be great and fun. Thanks
|
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Try sth like this... Code:
value=1
cal 10 1987 | awk -v var="$value" 'NR==2{n=split($0,P)}
NR>2{for(i=1;i<=7;i++){if(var == $i){print P[7-NF+i]}}}' |
| The Following User Says Thank You to pamu For This Useful Post: | ||
sathyaonnuix (12-25-2012) | ||
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Not sure if this is worth the while...: Code:
$ for DAY in {1..31}
do
cal 10 1987|awk -v day=$DAY \
'NR==1{next}
NR==2 {split ($0, WD); next}
match ($0, "(^| )"day" ") {print day,WD[ int((4+RSTART-RSTART%2)/3)]}
'
done
1 Th
2 FrYou can squeeze all of this into one single line, of course, as e.g. NR==1 can be omitted and the variables can be one letter only, but readability may suffer... see yourself: Code:
cal 10 1987|awk -v D=$1 'NR==2 {split($0,WD)} R=match($0,"(^| )"D" ") {print WD[int((4+R-R%2)/3)]}'Last edited by RudiC; 12-24-2012 at 09:41 AM.. |
|
#4
|
|||
|
|||
|
You might find the thread Find Day of Week useful.
|
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
Pamu, great work... Can you please explain me about the P in split command and the piece P[7-NF+i]
|
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
NR==2{n=split($0,P)} means when input record is 2nd (Days of Week) divide all of them and store in an array variable named
P Code:
October 1987
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 31P[7-NF+i] Is the formula pamu used to get the right array index & value to print of the day of week for date entered. |
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
Sorry bipinajith, one more query. I understood they are splitting the columns and storing each value into the array P. Whats the need of n here n=split($0,P).
---------- Post updated at 07:42 AM ---------- Previous update was at 07:31 AM ---------- @RudiC: Thanks for your code, can you please explain me on "(^| )"D" " How this works. @Don: I am looking after your thread |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Calender/docket software | kbweiss | UNIX and Linux Applications | 0 | 09-11-2012 03:11 PM |
| Awk with Find and play using Space | Arunprasad | Shell Programming and Scripting | 5 | 01-11-2012 04:19 PM |
| Question on Autosys calender date. | sravuri | Shell Programming and Scripting | 2 | 10-04-2011 10:38 PM |
| Script to find out first day of our calender | krishnampkkm | UNIX for Dummies Questions & Answers | 3 | 03-11-2010 11:03 PM |
| C Calender Help - Unusual error | Octal | Programming | 3 | 03-26-2007 03:08 PM |
|
|