How to get the number of days of difference between two dates in awk -


i need read date format file in awk. each line in file has 2 date data 1 in first element , other 1 in second element.the format day/month/year. there way of comparison between 2 dates in awk. instance, need 2 days answer if have 03/05/2015 , 05/05/2015.


sorry not providing sample input/output question. input same akshay described. have file in each line starts 2 dates

03/05/2015   05/05/2015 

corresponding start , end of process (the second 1 happens after first one). hard coding using awk read information 2 dates. not optimal solution needed obtain results yesterday. simply, extract day, month , year of each date , compute how many days far apart.
simple check see if corresponding year leap year or not.

awk'{  d1= int(substr($1,1,2));    d2= int(substr($2,1,2));     m1= int(substr($1,4,2));     m2= int(substr($2,4,2));     y1 = int(substr($1,7,4));     y2 = int(substr($2,7,4));  leap1 = ((y1-2008)%4)?1:0;  leap2 = ((y2-2008)%4)?1:0;  cnt1 = 0;  ii = 1;   while(ii<d1) { if(ii==2) cnt1+=(leap1+28); else{if((ii==1)||(ii==3)||(ii==5)||(ii==7)||(ii==8)||(ii==10)||(ii==12)) cnt1+=31; if((ii==4)||(ii==6)||(ii==9)||(ii==11)) cnt1+=30;}ii++;}  cnt2 = 0;  ii = 1;   while(ii<d2)   { if(ii==2) cnt2+=(leap2+28); else{if((ii==1)||(ii==3)||(ii==5)||(ii==7)||(ii==8)||(ii==10)||(ii==12)) cnt2+=31; if((ii==4)||(ii==6)||(ii==9)||(ii==11)) cnt2+=30;}ii++;}  day = cnt2-cnt1+d2-d1+(y2-y1)*365;  }` 

i know far optimal again needed produce quick.

i assume input looks this

input file

[akshay@localhost tmp]$ cat file 03/05/2015 05/05/2015 06/03/2015 08/05/2015 

script - gawk specific

[akshay@localhost tmp]$ cat date_diff_gawk.awk function date2time(date,     a) {         split(date, a, /\//)         return mktime(a[3] " " a[2] " " a[1] " 0 0 0") } function abs(n) {         return n<0 ? -n : n } function diff_days(d1,d2,    delta) {         delta = date2time(d1) - date2time(d2)         return int(abs(delta)/86400) } {     print $0, diff_days($2,$1) " days" } 

script - awk

[akshay@localhost tmp]$ cat date_diff_anyawk.awk begin {     split("0,31,59,90,120,151,181,212,243,273,304,334",d,/,/) } function dfm(date,   a,year,month,day) {         split(date,a,/\//)         year = a[3]; month = a[2]+0; day = a[1]         date = ((year - 1970) * 365.25) + d[month] + day - 1         if ((year % 4) == 0 && month > 2) { date = date + 1}         return (date * 86400 - (25200))/86400 } function abs(n) {         return n<0 ? -n : n } function date_diff(d1,d2) {     return  abs(dfm(d1)-dfm(d2)) } {     print $0,date_diff($2,$1) " days" } 

execution

[akshay@localhost tmp]$ awk -f date_diff_gawk.awk file 03/05/2015 05/05/2015 2 days 06/03/2015 08/05/2015 63 days  [akshay@localhost tmp]$ awk -f date_diff_anyawk.awk file 03/05/2015 05/05/2015 2 days 06/03/2015 08/05/2015 63 days  [akshay@localhost tmp]$ echo '06/03/2015 31/05/2015' | awk -f date_diff_any_awk.awk  06/03/2015 31/05/2015 86 days 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -