Awk/sed to play on calender


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk/sed to play on calender
# 8  
Old 12-25-2012
Awk split function returns the number of elements created which pamu stored in variable n. But this assignment is not required because this variable and its value is not used for any arithmetic operation. So you can avoid this and it will not make any difference.
Code:
cal 10 1987 | awk -v var="$value" 'NR==2{split($0,P)}
NR>2{for(i=1;i<=7;i++){if(var == $i){print P[7-NF+i]}}}'

# 9  
Old 12-25-2012
Quote:
Originally Posted by sathyaonnuix
. . .
@RudiC: Thanks for your code, can you please explain me on "(^| )"D" " - How this works.
This is to make sure day e.g. 1 will not match 11. The day (D) is surrounded by spaces or by line start and a trailing space. (^| ) is an ored regex used for this.
# 10  
Old 12-26-2012
Quote:
Originally Posted by sathyaonnuix
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).
As bipinajith already explained how the script works and the logic.
I'd like to change it's logic lit bit. Instead of hard coding with numbers.
You can use bipinajith's code from post 8 also.
OR
Code:
value=1

cal 10 1987 | awk -v var="$value" 'NR==2{n=split($0,P)}
NR>2{for(i=1;i<=n;i++){if(var == $i){print P[n-NF+i]}}}'

# 11  
Old 12-26-2012
Quote:
Originally Posted by pamu
As bipinajith already explained how the script works and the logic.
I'd like to change it's logic lit bit. Instead of hard coding with numbers.
You can use bipinajith's code from post 8 also.
OR
Code:
value=1

cal 10 1987 | awk -v var="$value" 'NR==2{n=split($0,P)}
NR>2{for(i=1;i<=n;i++){if(var == $i){print P[n-NF+i]}}}'

To see why this might not be a good idea, try setting value to 30 or 31 and use cal 12 2012 instead of cal 10 1987.
# 12  
Old 12-26-2012
Quote:
Originally Posted by Don Cragun
To see why this might not be a good idea, try setting value to 30 or 31 and use cal 12 2012 instead of cal 10 1987.
Hi Don,

Thanks for pointing out the problem.Smilie

Code:
cal 12 2012 | awk -v var="$value" 'NR==2{n=split($0,P)}
NR>3{n=NF}
NR>2{for(i=1;i<=n;i++){if(var == $i){print P[n-NF+i]}}}'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

2. Shell Programming and Scripting

Facing problem while having time popup from inline calender

I have CGI Perl script that contains date column and date popup will be displayed from inline calender. I had a html script for the same and converted the same to CGI script. html page worked fine but no luck with CGI script. Could anyone please look into the below script and let me know... (1 Reply)
Discussion started by: scriptscript
1 Replies

3. Shell Programming and Scripting

Enter number of days and get calender

Friends need assistance in getting a script either on shell or perl. Below is the situation Taking Today's calender into consideration with Month,Day,Year current .Using that i would like give number of days to get its month,day,year for future or past calender depending on the + or - days... (8 Replies)
Discussion started by: ajayram_arya
8 Replies

4. UNIX and Linux Applications

Calender/docket software

We are running in a Linux/Samba environment. Can anyone suggest calendar/docket software that will run in our environment? (0 Replies)
Discussion started by: kbweiss
0 Replies

5. Shell Programming and Scripting

Awk with Find and play using Space

Hi All, Sample records 2157 91128 -rw-r----- 1 arun1 staff 93315072 Aug 23 06:44 /home/arun/my own/file_name.txt 2157 91128 -rw-r----- 1 arun1 staff 93315072 Aug 23 06:44 /home/arun/myown/file name2.txt i want to print only user name, user group, size, date time stamp, and... (5 Replies)
Discussion started by: Arunprasad
5 Replies

6. Shell Programming and Scripting

Question on Autosys calender date.

Hi I am trying to schedule a job through Autosys through UNIX on a particular day of every month (for example 20th of every month). Can some one please help me whats the command or whats the process to run on that particular day of month. Thank you, (2 Replies)
Discussion started by: sravuri
2 Replies

7. Fedora

Script to find out first day of our calender

I try to find the first day of our calender. So I used this script ... echo -n "The week of the date 01jan0001 : " echo -n `date -d 00010101 +%A` echo But its shows error bash-3.1$ sh first_day.shThe week of the date 01jan0001 : date: invalid date `00010101' (3 Replies)
Discussion started by: krishnampkkm
3 Replies

8. Shell Programming and Scripting

Calender Unix programming date issues

Hi, i;m beginner of Unix, i trying to use crontab to zip my log file automatically, below is my coding, some of the statement i don't know whether is correct or not. Pls help:) year=`date '+%Y'` month=`date '+%m'` day=`date '+%d'` day=`expr $day - 1` case $month in 1 | 3 | 5 | 7 | 8 | 9 |... (4 Replies)
Discussion started by: dannyd_y
4 Replies

9. Shell Programming and Scripting

convert Julian date to calender date

Hi, I have script in unix which creates a julian date like 126 or 127 I want convert this julian date into calender date ex : input 127 output 07/may/2007 or 07/05/2007 or 07/05/07 rgds srikanth (6 Replies)
Discussion started by: srikanthus2002
6 Replies

10. Programming

C Calender Help - Unusual error

I'm making a program that you input the month and year, and it creates a calender for that month of that year. This is my largest project yet, and I broke it up into several source files. cal.c #include "cal.h" #include <stdio.h> main() { int month, year; scanf("%d %d", &month,... (3 Replies)
Discussion started by: Octal
3 Replies
Login or Register to Ask a Question