Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Get number of days between 2 dates

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-12-2017   -   Original Discussion by jiam912
jiam912's Unix or Linux Image
jiam912 jiam912 is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 18 November 2017, 9:21 PM EST
Location: EEUU
Posts: 353
Thanks: 269
Thanked 1 Time in 1 Post
Get number of days between 2 dates

Gents.
Please can u help.
I would like to calculate the days between two dates.
Example file1 ( previous date)
file1 - Input file

Code:
9/29/2010 10195
9/29/2010 1057
2/2/2016 10
2/2/2016 10169
2/2/2016 1057
2/3/2016 10005
2/3/2016 10014

In file2 I add the actual date using this code.

Code:
awk -v date="$(date +"%m/%d/%Y")" '{print $0, date}' file1 > file2

file2

Code:
9/29/2010 10195 05/12/2017
9/29/2010 1057 05/12/2017
2/2/2016 10 05/12/2017
2/2/2016 10169 05/12/2017
2/3/2016 10005 05/12/2017
2/3/2016 10014 05/12/2017

then using this code i get the difference ( number of days) for 2 dates

Code:
awk -v q='"' 'BEGIN {OFS= ","}{
 c="date -d"q$1q" +%s";c|getline d1;close(c); 
 c="date -d"q$3q" +%s";c|getline d2;close(c);
 printf "%s, %s, %s, %d\n", $1, $2, $3,(d2-d1)/3600/24}' file2  > file3

file3 - Output file, Column 4 is the days between 2 dates.

Code:
9/29/2010, 10195, 05/12/2017, 2417
9/29/2010, 1057, 05/12/2017, 2417
2/2/2016, 10, 05/12/2017, 464
2/2/2016, 10169, 05/12/2017, 464
2/3/2016, 10005, 05/12/2017, 463
2/3/2016, 10014, 05/12/2017, 463

It works fine, BUT take a lot time to end.. My input file is a big file and it take very long time to end.

Kindly can you check the code and if it is possible modify it to be faster. Get same output (file3) but more faster.

Appreciate your help
Sponsored Links
    #2  
Old Unix and Linux 05-12-2017   -   Original Discussion by jiam912
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 November 2017, 9:22 AM EST
Location: Aachen, Germany
Posts: 11,625
Thanks: 320
Thanked 3,616 Times in 3,321 Posts
Do the links given at the bottom left under "More UNIX and Linux Forum Topics You Might Find Helpful" help you?
The Following User Says Thank You to RudiC For This Useful Post:
jiam912 (05-12-2017)
Sponsored Links
    #3  
Old Unix and Linux 05-12-2017   -   Original Discussion by jiam912
jiam912's Unix or Linux Image
jiam912 jiam912 is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 18 November 2017, 9:21 PM EST
Location: EEUU
Posts: 353
Thanks: 269
Thanked 1 Time in 1 Post
Hi RudiC.

Already I look in the More Unix and Linux Forum topics..

But nothing to help..Linux
    #4  
Old Unix and Linux 05-12-2017   -   Original Discussion by jiam912
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 November 2017, 9:22 AM EST
Location: Aachen, Germany
Posts: 11,625
Thanks: 320
Thanked 3,616 Times in 3,321 Posts
No surprise it is taking its time, creating two processes per line for a large file. Try - provided you have a recent bash which you fail to mention -

Code:
date -f <(cut -d" " -f1 file2) +"($(printf "%(%s)T\n") - %s)/86400" | bc | paste file2 -
9/29/2010 10195 05/12/2017    2417
9/29/2010 1057 05/12/2017    2417
2/2/2016 10 05/12/2017    465
2/2/2016 10169 05/12/2017    465
2/3/2016 10005 05/12/2017    464
2/3/2016 10014 05/12/2017    464

The Following User Says Thank You to RudiC For This Useful Post:
jiam912 (05-12-2017)
Sponsored Links
    #5  
Old Unix and Linux 05-12-2017   -   Original Discussion by jiam912
jiam912's Unix or Linux Image
jiam912 jiam912 is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 18 November 2017, 9:21 PM EST
Location: EEUU
Posts: 353
Thanks: 269
Thanked 1 Time in 1 Post
Hi RudiC.

I got :

Code:
9/29/2010 10195 05/12/2017    -14880

Sponsored Links
    #6  
Old Unix and Linux 05-12-2017   -   Original Discussion by jiam912
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 November 2017, 9:22 AM EST
Location: Aachen, Germany
Posts: 11,625
Thanks: 320
Thanked 3,616 Times in 3,321 Posts
OK - what would you suggest? Looking at my sample output, comparing it to yours?
The Following User Says Thank You to RudiC For This Useful Post:
jiam912 (05-12-2017)
Sponsored Links
    #7  
Old Unix and Linux 05-12-2017   -   Original Discussion by jiam912
jiam912's Unix or Linux Image
jiam912 jiam912 is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 18 November 2017, 9:21 PM EST
Location: EEUU
Posts: 353
Thanks: 269
Thanked 1 Time in 1 Post
RudiC
The problem for me is here

Code:
date -f <(cut -d" " -f1 file2) +"($(printf "%(%s)T\n") - %s)/86400" | bc | paste file2 -

This value returns 0 for timestamp current date, here shoulbe value = 1494587900..
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calculate the number of days between 2 dates - bash script kovacsakos Shell Programming and Scripting 3 09-13-2012 07:49 PM
Calculate the number of days between 2 dates - PHP kovacsakos Web Programming 3 03-23-2012 07:34 AM
Get number of days between given dates suryanarayana Shell Programming and Scripting 4 04-25-2010 09:43 AM
Number of days between two distinct dates radheymohan UNIX for Advanced & Expert Users 1 10-16-2007 09:39 AM
Find number of days and list out the dates in between Raynon Shell Programming and Scripting 2 02-12-2007 08:52 PM



All times are GMT -4. The time now is 10:23 AM.