Subtract 2 date columns in .csv file and get output as number of days


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Subtract 2 date columns in .csv file and get output as number of days
# 1  
Old 01-07-2013
Subtract 2 date columns in .csv file and get output as number of days

Hi,

I have one .csv file. I have 2 date columns present in file, column 2 and column 3.

I need to calculate how many days exist between 2 dates.

I am trying to subtract date column 2 from date column 3.
Eg: my file look likes

s.no, Start_date,End_Date
Code:
1, 7/29/2012,10/27/2012
2, 7/29/2012,8/11/2012

my output should be
Code:
90
13

like this.

I am not able to get command to calculate how many days exist between these 2 dates.
after that, As i need to check how many sundays exists between the dates. so I will divide number of days/7.
Please help.

Last edited by radoulov; 01-08-2013 at 04:49 AM..
# 2  
Old 01-07-2013
Check out the following earlier posting in this forum: Yesterdays Date/Date Arithmetic

Then note that dividing the number of days by 7 will not tell you how many Sundays are between two dates unless the number of days is evenly divisible by 7. As an example, the number of Sundays between 12/29/2012 and 12/31/2012 is 1, but the number of Sundays between 1/29/2013 and 1/31/2013 is 0 even though the number of days between the start and end date is the same in both cases.
# 3  
Old 01-08-2013
Can anyone please provide the simple function to subract date column 2 from date column 3 to get number of days as given in example above.

Thanks
# 4  
Old 01-08-2013
It depends on what date utility you're using. If you're on a system where the desciprtion of the -d option on the date man page is something like:
Code:
     -d dst  Set the kernel's value for daylight saving time.  If dst is non-
             zero, future calls to gettimeofday(2) will return a non-zero for
             tz_dsttime

or if there is no -d option, you need to use one of the methods on the web site I referred you to in an earlier message in this thread. If you're on a system where the date man page description is something like:
Code:
       -d, --date=STRING
	      display time described by STRING, not `now'

Then you can use:
Code:
date -d "$value" "+%s"

to get the time in seconds for the given dates specifed by having $value set to the second field on the two lines you're talking about and then convert the seconds since the Epoch to days since the Epoch and subtract. You can use "+%a" to get the abbreviated weekday name corresponding to the given dates.

Neither -d nor +%s are defined by the standards, so any use of either of these is not portable between different systems.
# 5  
Old 01-08-2013
Thanks for the help.

when I used this format like this:
Code:
date -d "12/12/2012" +%s

It's giving me result. But i am not able to give my file name with this format as need to change 2nd date column value into number from each line of my file.
Suppose my file name is File1.csv
I tried this

date -d "$2" +%s File1.csv. But it's not working.

Last edited by radoulov; 01-08-2013 at 04:32 AM..
# 6  
Old 01-08-2013
Quote:
Originally Posted by Dimple
Thanks for the help.

when I used this format like this:
date -d "12/12/2012" +%s
It's giving me result. But i am not able to give my file name with this format as need to change 2nd date column value into number from each line of my file.
Suppose my file name is File1.csv
I tried this

date -d "$2" +%s File1.csv. But it's not working.
Since there are spaces in the string you want output, you need quotes. Try:
Code:
date -d "$2" "+%s File1.csv"

This should work, but I can't test it on the systems available to me for testing. (None of them define the date -d option this way and none of them support %s as an output string format specifier.)
# 7  
Old 01-08-2013
when I am using this function it's giving only 1 output as

1357624800 test1.csv

But I have around 100 records in my file and all records have date given in 2nd column

I should get list of numbers as output e.g.

1357624800
1357624822 like this. But getting wrong output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

2. Shell Programming and Scripting

Get a given date and subtract it to 5 days ago

Hi all, I have been researching to obtain SSL certification expiry for most of our webistes. For some cases, some hosts where not directly accessible so i finally got a solution working with curl using my proxy. This lists the expiry date which i'm finally looking for. # curl --proxy... (4 Replies)
Discussion started by: nms
4 Replies

3. Shell Programming and Scripting

Help to subtract columns from 2 files and output to new file

Hi, I have 2 files in below formats File1_Stored.txt ABC:100, 83 ABC:84, 53 ABC:14, 1222And File2_Stored.txt ABC:100 , 83 ABC:84 , 1553 ABC:524 , 2626I am trying to get the 3rd file in below format. So, whenever difference is 0 it shouldn't appear but if the difference is not 0 then... (2 Replies)
Discussion started by: Abhayman
2 Replies

4. Shell Programming and Scripting

Subtract months/days from date

Hi, Can you please let me know code for the below (in korn shell) a) Subtract month(s) from given date b) Subtract day(s) from give date c) Subtract month(s) from given timestamp d) Subtract day(s) from give timestamp (1 Reply)
Discussion started by: tostay2003
1 Replies

5. UNIX for Advanced & Expert Users

Subtract days to a date in AIX 5.3

good afternoon, can someone help me, I need to make a script where n subtract days to a date. I am using AIX 5.3. Greetings. (4 Replies)
Discussion started by: systemoper
4 Replies

6. Shell Programming and Scripting

perl : number to date conversion in CSV file

I have a CSV file in the below format. while generating CSV file from excel sheet , date in excel sheet(Format :Mon 8/28/2012) got converted into the below format with numbers 41148,41149 so on. Could anyone please let me know how to the convert the numbers(41148,41149 so on.) to its actual... (2 Replies)
Discussion started by: giridhar276
2 Replies

7. Shell Programming and Scripting

How to subtract a number from all columns?

Hi, I want to subtract a number from all columns except the first column. I have a number of files each having different columns around 60/70. How to do that in awk or any other command? Thanks Input Col 1 Col 2 Col3 - - - - Col55 1 .0123 .098 - - - 0.6728 2 - -... (3 Replies)
Discussion started by: Surabhi_so_mh
3 Replies

8. Shell Programming and Scripting

Number of days between the current date and user defined date

I am trying to find out the number of days between the current date and user defined date. I took reference from here for the date2jd() function. Modified the function according to my requirement. But its not working properly. Original code from here is working fine. #!/bin/sh... (1 Reply)
Discussion started by: hiten.r.chauhan
1 Replies

9. Shell Programming and Scripting

Need help to subtract columns from 2 files and output to new file

Hi, I need some help figuring this out, I think it can be done using awk but I don't know how. So, I want to take two input files, subtract some columns with each other and then output to a new results file. InFile1.txt AAA 100 200 BBB CCC 300 400 DDD InFile2.txt AAA 50 60 BBB CCC 70... (7 Replies)
Discussion started by: MrTrigger
7 Replies

10. Shell Programming and Scripting

Subtract days from a variable holding date

Hi, could someone help on this.. I have a date in variable procdate="05/30/2009" I would want to Subtract it with 3 or 4 (2 Replies)
Discussion started by: infernalhell
2 Replies
Login or Register to Ask a Question