Awk/sed to play on calender | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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 !!

Awk/sed to play on calender

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-24-2012
sathyaonnuix's Avatar
sathyaonnuix sathyaonnuix is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 26 June 2014, 12:23 PM EDT
Posts: 129
Thanks: 35
Thanked 14 Times in 13 Posts
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  31

When 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  
Old 12-24-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
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  
Old 12-24-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 25 October 2014, 5:15 AM EDT
Location: Aachen, Germany
Posts: 4,425
Thanks: 73
Thanked 1,082 Times in 1,020 Posts
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 Fr

You 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  
Old 12-24-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 25 October 2014, 5:28 AM EDT
Location: San Jose, CA, USA
Posts: 4,900
Thanks: 182
Thanked 1,646 Times in 1,397 Posts
You might find the thread Find Day of Week useful.
Sponsored Links
    #5  
Old 12-25-2012
sathyaonnuix's Avatar
sathyaonnuix sathyaonnuix is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 26 June 2014, 12:23 PM EDT
Posts: 129
Thanks: 35
Thanked 14 Times in 13 Posts
Quote:
Originally Posted by pamu View Post
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]}}}'

Pamu, great work... Can you please explain me about the P in split command and the piece P[7-NF+i]
Sponsored Links
    #6  
Old 12-25-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 October 2014, 8:12 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts
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 31

P[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  
Old 12-25-2012
sathyaonnuix's Avatar
sathyaonnuix sathyaonnuix is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 26 June 2014, 12:23 PM EDT
Posts: 129
Thanks: 35
Thanked 14 Times in 13 Posts
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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 10:32 AM.