I have the following file:
cat x.txt
2020-01-04
2020-01-01
2008-03-12
2021-08-09
I am trying to write an awk script that outputs the following:
2020-01-04 2022-03-09 795
2020-01-01 2022-03-09 798
2008-03-12 2022-03-09 5110
2021-08-09 2022-03-09 212
Where column 2 is the current date and column 3 is the difference between column 1 and 2 in days. I have started a script like this but not really getting it:
cat y
#!/usr/bin/env bash
awk '
BEGIN {
FS = OFS = "\t"
str = "date +%Y-%m-%d"
str | getline date
d2 = mktime((gensub(/-/, " ", "g", date)) " 0 0 0")
d1 = mktime((gensub(/-/, " ", "g", $1)) " 0 0 0")
}
{
print $1, date, d2 - d1
}
' "${@:--}"
When I run this I get the following:
./y x.txt
2020-01-04 2022-03-09 1646737201
2020-01-01 2022-03-09 1646737201
2008-03-12 2022-03-09 1646737201
2021-08-09 2022-03-09 1646737201
I am not sure how to work with dates so help is much appreciated.