Sponsored Content
Top Forums Shell Programming and Scripting how to increment days according to year & month Post 302391394 by Amit.Sagpariya on Monday 1st of February 2010 08:13:04 AM
Old 02-01-2010
Hi Reva,

Code:
#!/bin/ksh
let first_year=`head -1 a.dat | awk '{print $2}'`
cat a.dat | while read abc
do
 f_str=`echo $abc | awk '{print $1, $2, $3}'`
 b_str=`echo $abc | awk '{print $5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21}'`
 let year_num=`echo $abc | awk '{print $2}'`
        let month_num=`echo $abc | awk '{print $3}'`
        let month_day=`echo $abc | awk '{print $4}'`
 let diff_year=`echo "$year_num - $first_year" | bc`
 let out_year=`echo "$diff_year * 365" | bc`
        if [ $month_num -eq 1 ]
        then
                let out_month_day=`echo "$month_day + $out_year" | bc`
        elif [ $month_num -eq 2 ]
        then
                let out_month_day=`echo "$out_year + 31 + $month_day" | bc`
        elif [ $month_num -eq 3 ]
        then
                let out_month_day=`echo "$out_year + 31 + 28 + $month_day" | bc`
        elif [ $month_num -eq 4 ]
        then
                let out_month_day=`echo "$out_year + 31 + 28 + 31 + $month_day" | bc `
        elif [ $month_num -eq 5 ]
        then
                let out_month_day=`echo "$out_year + 31 + 28 + 31 + 30 + $month_day" | bc`
        elif [ $month_num -eq 6 ]
        then
                let out_month_day=`echo "$out_year + 31 + 28 + 31 + 30 + 31 + $month_day" | bc`
        elif [ $month_num -eq 7 ]
        then
                let out_month_day=`echo "$out_year + 31 + 28 + 31 + 30 + 31 + 30 + $month_day" | bc`
        elif [ $month_num -eq 8 ]
        then
                let out_month_day=`echo "$out_year + 31 + 28 + 31 + 30 + 31 + 30 + 31 + $month_day" | bc`
        elif [ $month_num -eq 9 ]
        then
                let out_month_day=`echo "$out_year + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + $month_day" | bc`
        elif [ $month_num -eq 10 ]
        then
                let out_month_day=`echo "$out_year + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + $month_day" | bc`
        elif [ $month_num -eq 11 ]
        then
                let out_month_day=`echo "$out_year + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + $month_day" | bc`
        elif [ $month_num -eq 12 ]
        then
                let out_month_day=`echo "$out_year + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + $month_day" | bc`
        fi
        echo " ${f_str} ${out_month_day} ${b_str}" >> b.dat
 
 
done

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script for tar and zip based on month & year

Hi Friends, I'm doing on script which finds all the files with time stamp and makes them tar and zip, based on their respective month&year. for instance "mar-2004.tar.zip" will contain all the files which was created/accessed/modified on mar-2004. like this the entire filesystem should be taken... (1 Reply)
Discussion started by: tuxfello
1 Replies

2. UNIX for Advanced & Expert Users

how to get the last month and year in UNIX

how to get the last month and year in UNIx (2 Replies)
Discussion started by: Vijay06
2 Replies

3. Programming

Interval year to month

Hi, I'm working on a Informix4gl module. I'm just trying to find out any built-in function to fetch only the year/month from an INTERVAL YEAR TO MONTH data value. Please let me know, if there are any functions to do this. If not, let me know for any alternative solutions to attain this. ... (5 Replies)
Discussion started by: dvah
5 Replies

4. Shell Programming and Scripting

want to get last year and month from the file

Hi I have files like abc_cd_20110302_123423 abc_cd_ef_20110301_123423 abc_cd_ef_20110403_123423 abc_ef_20110401_123423 I want to extract the the year and month associated with each file. I tried logfileyearmonth=`echo $logfile | awk -F_'{print $NF}'` Any other way can I... (6 Replies)
Discussion started by: dgmm
6 Replies

5. UNIX for Dummies Questions & Answers

Simpler next month year program

I have created this program to get the next month and year. Is there a simpler way. #!/bin/ksh string=`cat Date.txt` year=`echo $string | cut -c 1-4` month=`echo $string | cut -c 5-6` echo $year$month mon=`expr $month + 1` if ; then mon=0$mon echo $mon fi if ; then month=01 ... (2 Replies)
Discussion started by: w020637
2 Replies

6. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

7. Shell Programming and Scripting

How to find all files for same month and year?

Hi All, I find all files for same month and year lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 c.txt -rwxr-xr-x 1 user userg 1596 Mar 21 2012 d.txt -rwxr-xr-x 1 user userg 1596 Mar 22 2012 f.txt -rwxr-xr-x 1... (8 Replies)
Discussion started by: Makarand Dodmis
8 Replies

8. Shell Programming and Scripting

How to find all files other than first two dates & last date per month and year?

how to find all files other than first two dates & last date per month and year Hi All, lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 a.txt -rwxr-xr-x 1 user userg 1596 Mar 19 2012 b.txt -rwxr-xr-x 1 user userg ... (6 Replies)
Discussion started by: Makarand Dodmis
6 Replies

9. UNIX for Beginners Questions & Answers

Best way to increment weeks based on fiscal start year

Hi Folks - I'm looking for the best way to to increment fiscal weeks - allow me to explain. At my one client, 10/01/17 was the beginning if year fiscal year 2018. Each week, I need to manage a unique set of variable that are updated in my application - they are called substitution variables.... (31 Replies)
Discussion started by: SIMMS7400
31 Replies
CALENDAR(3)						   BSD Library Functions Manual 					       CALENDAR(3)

NAME
easterg, easterog, easteroj, gdate, jdate, ndaysg, ndaysj, week, weekday -- Calendar arithmetic for the Christian era LIBRARY
Calendar Arithmetic Library (libcalendar, -lcalendar) SYNOPSIS
#include <calendar.h> struct date * easterg(int year, struct date *dt); struct date * easterog(int year, struct date *dt); struct date * easteroj(int year, struct date *dt); struct date * gdate(int nd, struct date *dt); struct date * jdate(int nd, struct date *dt); int ndaysg(struct date *dt); int ndaysj(struct date *dt); int week(int nd, int *year); int weekday(int nd); DESCRIPTION
These functions provide calendar arithmetic for a large range of years, starting at March 1st, year zero (i.e., 1 B.C.) and ending way beyond year 100000. Programs should be linked with -lcalendar. The functions easterg(), easterog() and easteroj() store the date of Easter Sunday into the structure pointed at by dt and return a pointer to this structure. The function easterg() assumes Gregorian Calendar (adopted by most western churches after 1582) and the functions easterog() and easteroj() compute the date of Easter Sunday according to the orthodox rules (Western churches before 1582, Greek and Russian Orthodox Church until today). The result returned by easterog() is the date in Gregorian Calendar, whereas easteroj() returns the date in Julian Calendar. The functions gdate(), jdate(), ndaysg() and ndaysj() provide conversions between the common "year, month, day" notation of a date and the "number of days" representation, which is better suited for calculations. The days are numbered from March 1st year 1 B.C., starting with zero, so the number of a day gives the number of days since March 1st, year 1 B.C. The conversions work for nonnegative day numbers only. The gdate() and jdate() functions store the date corresponding to the day number nd into the structure pointed at by dt and return a pointer to this structure. The ndaysg() and ndaysj() functions return the day number of the date pointed at by dt. The gdate() and ndaysg() functions assume Gregorian Calendar after October 4, 1582 and Julian Calendar before, whereas jdate() and ndaysj() assume Julian Calendar throughout. The two calendars differ by the definition of the leap year. The Julian Calendar says every year that is a multiple of four is a leap year. The Gregorian Calendar excludes years that are multiples of 100 and not multiples of 400. This means the years 1700, 1800, 1900, 2100 are not leap years and the year 2000 is a leap year. The new rules were inaugurated on October 4, 1582 by deleting ten days following this date. Most catholic countries adopted the new calendar by the end of the 16th century, whereas others stayed with the Julian Calendar until the 20th century. The United Kingdom and their colonies switched on September 2, 1752. They already had to delete 11 days. The function week() returns the number of the week which contains the day numbered nd. The argument *year is set with the year that contains (the greater part of) the week. The weeks are numbered per year starting with week 1, which is the first week in a year that includes more than three days of the year. Weeks start on Monday. This function is defined for Gregorian Calendar only. The function weekday() returns the weekday (Mo = 0 .. Su = 6) of the day numbered nd. The structure date is defined in <calendar.h>. It contains these fields: int y; /* year (0000 - ????) */ int m; /* month (1 - 12) */ int d; /* day of month (1 - 31) */ The year zero is written as "1 B.C." by historians and "0" by astronomers and in this library. SEE ALSO
ncal(1), strftime(3) STANDARDS
The week number conforms to ISO 8601: 1988. HISTORY
The calendar library first appeared in FreeBSD 3.0. AUTHORS
This manual page and the library was written by Wolfgang Helbig <helbig@FreeBSD.org>. BUGS
The library was coded with great care so there are no bugs left. BSD
November 29, 1997 BSD
All times are GMT -4. The time now is 08:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy