I have file with values in below format-
datecolumn1 datecolumn2
20140202 20180113
20140202 20180113
20140202 20180113
20131202 20180113
20140331 20180113
I want to subtract $3-$2 to find total number of days. output will be as below-
20180113 20180115 3.
please help with awk command.
Following
awk
may help you in same, to get the difference of days between dates(format which you have posted in your post). Also this code is created and tested in GNUawk
.Note that the above is an approximation to the number of days difference but it does not account accurately for DST. I just used it since you seem happy with that approximation but if you wanted the result to be accurate you'd need a different time calculation that would involve using
strftime("%j")
for the start date, the last day of that year minus that value (to get the number of days remaining that year), the last day of every year between the start and end dates, and the day of the year for the end date.Using GNUawk,
mkdate
andFIELDWIDTHS
for separating the the date parts. As$4
is a space, it is abused as a space inmktime
as it is shorter to write than" "
(:(I was not really sure if
20180113 20180115
should produce3
as in the example or2
as math dictates.)