LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 07-04-2010, 05:35 PM   #1
btacuso
Member
 
Registered: May 2009
Posts: 32

Rep: Reputation: 15
How to subtract two dates in a file to get number of days?


I have a sample file with six fields delimited by commas below:

1astname1,firsname1 , 216057926,102,07/03/10,07/04/10
lastname2,firstname2 , 211968262,105,07/01/10,07/04/10
lastname3,firstname3, 214783220,113,06/29/10,07/04/10

field 5 is first date
field 6 is last date

I need to subtract field6 - field5 to get the number of days and then append it as field7 to the file. Desired result is below:

1astname1,firsname1 , 216057926,102,07/03/10,07/04/10,1
lastname2,firstname2 , 211968262,105,07/01/10,07/04/10,3
lastname3,firstname3, 214783220,113,06/29/10,07/04/10,5

I appreciate any help.
 
Old 07-04-2010, 06:04 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
You can simply use the date command and the bash arithmetic operator. Date differences are usually made in seconds since epoch units, then we can apply any conversion to days, hours and so on by means of the proper conversion factor. Since you don't need fractional parts of the day, bash (integer) arithmetic is enough. An example of the date command:
Code:
$ my_date="07/04/10"
$ date -ud $my_date +%s
1278201600
Note the -u option to work with UTC time. It's useful if by chance the two dates span across the day of change from/to Daylight Saving Time.
 
Old 07-13-2010, 11:33 AM   #3
btacuso
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Thanks a lot. I was able to fix it thru this guide.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to processing the log file within certain dates based on the file name shyork2001 Linux - General 1 04-08-2010 03:35 PM
How to subtract days HyperTrey Programming 15 06-06-2008 03:45 AM
How do I calculate the number of days from a particular date using the <cal> utility? deepumnit Linux - Desktop 3 12-31-2007 12:12 PM
file dates digitalgravy Linux - General 2 01-20-2005 10:58 AM
File Dates Rv5 Programming 5 09-07-2004 05:59 PM


All times are GMT -5. The time now is 02:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration