I extracted 2 date columns from a text file and I want to create a new file with the date difference (number of days) for each row. Manually inputing the dates to the command line using datediff
(from https://github.com/hroptatyr/dateutils) works (e.g. ddiff 2014-03-17 2013-09-19
returns -179
). There are many rows and is not feasible to do it manually.
Piping the file content cat dates_file.txt | ddiff
and also through xargs cat dates_file.txt | xargs | ddiff
gets me ddiff: Error: reference DATE must be specified
. What would be a good approach?
dates_file.txt
2014-02-03 2013-11-29
2015-12-07 2015-05-13
2015-01-23 2014-07-31
2012-02-28 2012-01-17
2014-03-17 2013-09-19
...
expected output
-66
-208
-176
-42
-179
...
To use ddiff
with xargs
:
xargs -n 2 ddiff < dates_file.txt
Alternatively, with perl
and the Date::Calc
module:
perl -MDate::Calc -ne '@a = /^(\d{4})-(\d\d)-(\d\d)\s+(\d{4})-(\d\d)-(\d\d)$/ or die; print Date::Calc::Delta_Days(@a), "\n"'
Provide either the file in argument after the command, or the data to the standard input.
This does error checking with or die
in case some lines do not have the expected format. Adapt the regexp or the script if the file format may be different...