how to increment days according to year & month


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to increment days according to year & month
# 1  
Data how to increment days according to year & month

Hiii i have a file with data as shown below:
a.dat:
HTML Code:
   RAO 1900  2  7  0  0  0.00  10.8000  76.8000  10.0   0  0.00   0  6.00  0.00  0.00   0  0.00  6.00   0   NULL
   LEE 1901  2 15  0  0  0.00  26.0000 100.0000   0.0   0  0.00   0  0.00  0.00  0.00   0  6.00  6.00   0   NULL
   RAO 1901  4 27  0  0  0.00  12.0000  75.0000  10.0   0  0.00   0  5.00  0.00  0.00   0  0.00  5.00   0   NULL
   SSR 1901 12  1  0  0  0.00  37.8000  66.2000  14.0   0  0.00   0  4.60  0.00  0.00   0  0.00  4.60   0   NULL
   SSR 1902  4 17 21 10  0.00  40.0000  71.0000  30.0   0  0.00   0  5.80  0.00  5.90   0  5.70  5.90   0   NULL
   SSR 1902  8 12 17 16  0.00  39.5000  68.5000  35.0   0  0.00   0  6.00  0.00  6.20   0  5.90  6.20   0   NULL
   BDA 1902  8 22  3  0  0.00  40.0000  77.0000  60.0   0  0.00   0  0.00  0.00  0.00   0  8.60  8.60   0   NULL
   SIG 1902  8 22  3  1  0.00  39.8000  76.2000  40.0   0  0.00   0  0.00  0.00  0.00   0  8.10  8.10   1   NULL
   SIG 1902  8 30 21 50  0.00  37.0000  71.0000 200.0   0  0.00   0  0.00  0.00  0.00   0  7.70  7.70   0   NULL
   SSR 1902  9 20  6 32  0.00  38.5000  67.0000  40.0   0  0.00   0  6.20  0.00  6.30   0  6.10  6.30   0   NULL
   SSR 1902 10  6  9 15  0.00  36.5000  70.5000 200.0   0  0.00   0  7.20  0.00  6.50   0  7.10  7.20   0   NULL
   SSR 1902 12  4 22 18  0.00  37.8000  65.5000  20.0   0  0.00   0  4.90  0.00  0.00   0  0.00  4.90   0   NULL
   RAO 1903  1 14  0  0  0.00  24.0000  70.0000  10.0   0  0.00   0  6.00  0.00  0.00   0  0.00  6.00   0   NULL
   SSR 1903  1 20  8 24  0.00  37.0000  71.0000  30.0   0  0.00   0  5.50  0.00  0.00   0  0.00  5.50   0   NULL
   SSR 1903  4 19 13 25  0.00  37.0000  71.0000 160.0   0  0.00   0  6.90  0.00  6.20   0  6.90  6.90   0   NULL
   SSR 1903  5 16  6 18  0.00   5.3600  92.5000  11.0   0  0.00   0  4.50  0.00  0.00   0  0.00  4.50   0   NULL
   RAO 1903  5 17  0  0  0.00  23.0000  80.0000  10.0   0  0.00   0  4.30  0.00  0.00   0  0.00  4.30   0   NULL
   SSR 1903 10 19  3 10  0.00  39.3000  74.5000  25.0   0  0.00   0  6.20  0.00  0.00   0  0.00  6.20   0   NULL
   SSR 1904  2  4 21  0  0.00  40.0000  78.0000  30.0   0  0.00   0  6.10  0.00  5.90   0  6.20  6.20   0   NULL
The output should be as follows
b.dat:
HTML Code:
  RAO 1900  2   38  0  0  0.00  10.8000  76.8000  10.0   0  0.00   0  6.00  0.00  0.00   0  0.00  6.00   0   NULL
   LEE 1901  2  411  0  0  0.00  26.0000 100.0000   0.0   0  0.00   0  0.00  0.00  0.00   0  6.00  6.00   0   NULL
   RAO 1901  4  482  0  0  0.00  12.0000  75.0000  10.0   0  0.00   0  5.00  0.00  0.00   0  0.00  5.00   0   NULL
   SSR 1901 12  700   0  0  0.00  37.8000  66.2000  14.0   0  0.00   0  4.60  0.00  0.00   0  0.00  4.60   0   NULL
   SSR 1902  4  837 21 10  0.00  40.0000  71.0000  30.0   0  0.00   0  5.80  0.00  5.90   0  5.70  5.90   0   NULL
   SSR 1902  8  954 17 16  0.00  39.5000  68.5000  35.0   0  0.00   0  6.00  0.00  6.20   0  5.90  6.20   0   NULL
   BDA 1902  8  964  3  0  0.00  40.0000  77.0000  60.0   0  0.00   0  0.00  0.00  0.00   0  8.60  8.60   0   NULL
   SIG 1902  8  964  3  1  0.00  39.8000  76.2000  40.0   0  0.00   0  0.00  0.00  0.00   0  8.10  8.10   1   NULL
   SIG 1902  8  972 21 50  0.00  37.0000  71.0000 200.0   0  0.00   0  0.00  0.00  0.00   0  7.70  7.70   0   NULL
   SSR 1902  9  993  6 32  0.00  38.5000  67.0000  40.0   0  0.00   0  6.20  0.00  6.30   0  6.10  6.30   0   NULL
   SSR 1902 10 1009 6  9 15  0.00  36.5000  70.5000 200.0   0  0.00   0  7.20  0.00  6.50   0  7.10  7.20   0   NULL
   SSR 1902 12 1068 22 18  0.00  37.8000  65.5000  20.0   0  0.00   0  4.90  0.00  0.00   0  0.00  4.90   0   NULL
   RAO 1903  1 1109 0  0  0.00  24.0000  70.0000  10.0   0  0.00   0  6.00  0.00  0.00   0  0.00  6.00   0   NULL
   SSR 1903  1 1115  8 24  0.00  37.0000  71.0000  30.0   0  0.00   0  5.50  0.00  0.00   0  0.00  5.50   0   NULL
   SSR 1903  4 1204 13 25  0.00  37.0000  71.0000 160.0   0  0.00   0  6.90  0.00  6.20   0  6.90  6.90   0   NULL
   SSR 1903  5 1231  6 18  0.00   5.3600  92.5000  11.0   0  0.00   0  4.50  0.00  0.00   0  0.00  4.50   0   NULL
   RAO 1903  5 1232  0  0  0.00  23.0000  80.0000  10.0   0  0.00   0  4.30  0.00  0.00   0  0.00  4.30   0   NULL
   SSR 1903 10 1387  3 10  0.00  39.3000  74.5000  25.0   0  0.00   0  6.20  0.00  0.00   0  0.00  6.20   0   NULL
   SSR 1904  2 1495 21  0  0.00  40.0000  78.0000  30.0   0  0.00   0  6.10  0.00  5.90   0  6.20  6.20   0   NULL
i.e. Based on on year & month date must be incremented. For example: for 1901 it 365+31(january)+15 & so on etc...
Help me out if any idea
# 2  
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

# 3  
Thanks so much its working..Can you please explain me the code ..I understood the logic but the unix code i didnt understand properly.explain with steps..
thank you....
# 4  
If you want to use perl

Code:
my @mon_array = (0,31,28,31,30,31,30,31,31,30,31,30,31);
my $leap = 0; # ==> not a leap year
open my $fh ,'<' , "abc.txt" || die "$!";
while(<$fh>){
        chomp;
        my @cols = split("\t");  # change to the delimiter if needed
        my $year = $cols[1];

        $leap = 1 if ((!($year % 100) && !($year % 400)) || (($year % 100) && !($year % 4)));
        my $num_of_days_from_prev_months = 0;
        for my $i (1 .. ($cols[2]-1)){
                $num_of_days_from_prev_months += ($i ==2 ) ? (($leap == 1) ? 29 : 28) : $mon_array[$i];
        }
        my $final_days = ($year-1900) * 365 + $num_of_days_from_prev_months + $cols[3];
        for(my $i=0;$i<=$#cols;$i++){
                ($i ==3) ? print "$final_days\t" : print "$cols[$i]\t";
        }
        print "\n";
}
close $fh;

HTH,
PL
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #161
Difficulty: Easy
Application programmers are typically concerned only with interfaces in the application layer and often also in the transport layer, while the layers below are services provided by the TCP/IP stack in the operating system.
True or False?

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

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

8. 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

9. 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

Featured Tech Videos