Getting a specific date from cal output with AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting a specific date from cal output with AWK
# 1  
Old 12-16-2009
Getting a specific date from cal output with AWK

Hi guys!

I'll make this short... Is there any good way to get the day number that first matches the Monday column from the cal command output with awk (or any other text manipulator commands) ?

I'm sorry if my question wasn't clear at all.

For example...

One cal output would be

Code:
$ cal
       December 2009      
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

Now, what I really need to get is the number of the first Monday in the month (I might also need, for example, the 3rd number of the Friday column, or any other number related to day that I need from that output).... In this case that would be the 7. I cannot think of anything since I have to consider that Monday might also be 1 or 2 in the 3rd line.....

Any idea?? I need this in order to setup a backup script for crontab, since as you know crontab is a little bit dumb.... but that doesn't matter.


Any help will be appreciated, thank you all in advance Smilie


P.S.: Sorry for my bad English...
# 2  
Old 12-16-2009
This seems to do the trick:

Code:
cal -m 12 | awk '{ if (substr($0, 4, 2)*1) { print $2; exit } }'

7


Edit: Um. I should finish reading the question!

Last edited by Scott; 12-16-2009 at 05:36 PM..
# 3  
Old 12-16-2009
# 4  
Old 12-16-2009
Hi !

At first sight that gave a good output. I'll try applying it to what I need it for.

Perhaps I should think of another way of doing what I need, 'cause yeah as you say it's a bit messy...

Thank you alot scottn!!!


EDIT: Just saw your post Jim! I'm checking it right now.
# 5  
Old 12-16-2009
If you have Perl and the Date::Manip module installed you can try this:

Code:
$ perl -le 'use Date::Manip;print UnixDate(ParseDate("1st Monday in December 2009"), "%d");'
07

$ perl -le 'use Date::Manip;print UnixDate(ParseDate("3rd Friday in December 2009"), "%d");'
18

# 6  
Old 12-16-2009
Quote:
Originally Posted by Casey
Hi !

At first sight that gave a good output. I'll try applying it to what I need it for.

Perhaps I should think of another way of doing what I need, 'cause yeah as you say it's a bit messy...

Thank you alot scottn!!!


EDIT: Just saw your post Jim! I'm checking it right now.
The "bit messy" bit I already removed because as you said "at first sight...". Sorry about that Smilie
# 7  
Old 12-16-2009
Sorry scottn, I don't get your post and don't understand why you deleted it.. (maybe language misunderstood)

Jim, yep that's why I was I asking this, my problem is something similar to that thread, I'll be reviewing that command. I've seen alot of answers regarding this issue, but I couldn't make work any of them. However, I came across with a post in a another forum where someone posted something that Perderabo came up with, that is
Code:
--------------------------------------------------------------------------------
#! /usr/bin/ksh
day=$(date +%d)
if ((day between 1 and 7, 8 and 14, etc....)) ; then
exec the mksysb command
fi
exit 1
--------------------------------------------------------------------------------

Maybe putting that script in the crontab telling it ro be ran on every monday (for example) will work..

SFNYC, unfortunately I don't have (and cannot install since it's a client LPAR) Date::Manip module.


Thank you all for your fast answers, this is very helpful. Smilie

Best regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to output match and mismatch with count using specific fields

In the below awk I am trying output to one file those lines that match between $2,$3,$4 of file1 and file2 with the count in (). I am also trying to output those lines that are missing between $2,$3,$4 of file1 and file2 with the count of in () each. Both input files are tab-delimited, but the... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

awk to output specific matches in file

Using the attached file, the below awk command results in the output below: I can not seem to produce the desired results and need some expert help. Thank you :). awk -F'' ' { id += $4 value += $5 occur++ } END{ printf "%-8s%8s%8s%8s\n", "Gene", "Targets", "Average Depth", "Average... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

AWK specific output filename

Hi All, I'd like to create a specific output filename for AWK. The file I am processing with AWK looks like: output_081012.csv* 27*TEXT*1.0*2.0*3.0 where * is my delimeter and the first line of the file is the output filename i'd like to create is there a way to assign an awk... (10 Replies)
Discussion started by: LMSteed
10 Replies

4. Shell Programming and Scripting

Find and print specific date with awk

hi all I would like to help me find the problem with this script to find and print to the screen a specific date of a log file that I have on my server, the date it is received as the first argument in the script $ 1 Here I show you a few lines that made ​​the idea of ​​my log file: ****... (4 Replies)
Discussion started by: gilmore666
4 Replies

5. Shell Programming and Scripting

awk assign output of array to specific field-number

With this script i want to print the output to a specific field-number . Can anybody help? awk 'NR=FNR{split(FILENAME,fn,"_");nr=$2;f = $1} END{for (i=1;i<=f;i++) print i,$fn=nr}' input_5.csv input_6.csvinput_5.csv 4 135 5 185 6 85 11 30input_6.csv 1 90 3 58 4 135 7 60 8 55 10... (1 Reply)
Discussion started by: sdf
1 Replies

6. Shell Programming and Scripting

Center output of 'cal' command in terminal

I have a function to center output in a terminal. However It only works for variables. I would like to center the output of the command 'cal' or 'cal -3' in a terminal. I have tried: center `cal` CAL=`cal`; center $CAL cal > cal.txt; center `cat cal.txt` center "`exec cal`" To no avail,... (10 Replies)
Discussion started by: AlphaLexman
10 Replies

7. Shell Programming and Scripting

awk statement to find events from a specific date

I have a script which archives log file events which are 90-days old. Script works fine but I wanted some input on one aspect of this script. My nawk statement, bolded below, that removes events 90-days prior from today, I need it to find anything 90-days or older. The log file date pattern looks... (0 Replies)
Discussion started by: numele
0 Replies

8. Red Hat

how to print date/cal in /etc/motd on linux server

helo i want to show today date and calendar when v succesfuly login on the system on the linux server please orovide the sol for the same thanx :):):) (1 Reply)
Discussion started by: dodasajan
1 Replies

9. UNIX for Advanced & Expert Users

Get date from cal function

Cal 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 cal | awk '{print $7}' Sat 13 20 27 In the above output i am missing 6 because the first column is empty so it is shfting. How... (3 Replies)
Discussion started by: ukatru
3 Replies

10. Shell Programming and Scripting

Formating cal output

Hi Gurus, In my Cal output i want to cut the date of 2nd saturday how tyo achive this. for eg in the below output i need that second saturday 13 to be cut. crypto $ cal January 2007 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... (2 Replies)
Discussion started by: Krrishv
2 Replies
Login or Register to Ask a Question