1

I have the below file:

1,2,3,4,5,2015-10-13,7,8,2037-01-15
1,2,3,4,5,2015-09-05,7,8,2040-01-15
1,2,3,4,5,2015-09-15,7,8,2016-06-15
1,2,3,4,5,2015-10-13,7,8,2015-07-08
1,2,3,4,5,2015-11-02,7,8,2015-08-02

Using awk, How can I check the number of days between two dates ($6 and $9)?

For the last record, the result should be 90

is it doable? Thanks ...

2

From the question I assume you want to count all months as 30 days. In this case you don't need to use external tool (like date), just calculate number of days by yourself:

awk -F'[,-]' '{print 360*($6-$11)+30*($7-$12)+($8-$13)}' file

-7652
-8770
-270
95
90
2

Assuming gnu awk is ok here....

Here's one way, it gives the number of days difference. But that isn't strictly what you want (2015-11-02 thru 2015-08-02 is 92 days and you want a result of 90).

$ awk -F, '{ gsub(/-/," ",$0); print (mktime($6 " 00 00 00")-mktime($9 " 00 00 00"))/86400 }' file.txt
-7765.04
-8898.04
-274
97
92.0417
$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.