0

I have a large .txt file (100m+) rows with 2 duplicated date columns, delimited by comma. I am looking for an efficient way to subtract 1 day from the given date. If the given date is on the 1st of the month, this should be taken care of and output the 30th/31st of the previous month.

I'd like to rename the columns to startDate and endDate as well.

Input:

Date,Date
2013-12-21,2013-12-21
2013-01-15,2013-01-15
2019-12-09,2013-12-09
2007-04-01,2007-04-01
2011-01-01,2011-01-01
...

Desired Output:

startDate,endDate
2013-12-20,2013-12-21
2013-01-14,2013-01-15
2019-12-08,2013-12-09
2007-03-31,2007-04-01
2010-12-31,2011-01-01
...
tichy
  • 99
  • 5
  • It's usually easiest to use the epoch as a reference. `date +%s` - once you have all dates in seconds (relative to the epoch) and durations in the same unit, making calculations usually falls into place – Ted Lyngmo Apr 14 '22 at 17:01
  • Bash itself doesn't give you this feature -- `date` is provided by your operating system, and is not part of the shell itself. Thus, your question should at least specify whether you're targeting GNU date only, or if you need compatibility with BSD/other tools. – Charles Duffy Apr 14 '22 at 17:03
  • Do note that each question should be about exactly one thing. "How do I do date arithmetic?" is one question. "How do I replace the first line of an input stream in a pipeline?" is a _second_ question. – Charles Duffy Apr 14 '22 at 17:06
  • @TedLyngmo I get `date: illegal option -- d` while doing so in Mac – tichy Apr 14 '22 at 17:21
  • @richtigga You tagged it `bash` so, `date +%s` shouldn't present a problem but what if you do `date '+%s'` and `date --version`, what does those report? – Ted Lyngmo Apr 14 '22 at 17:22
  • @TedLyngmo `date '+%s'` gives `1649957394`, and `date --version` gives `date: illegal option` – tichy Apr 14 '22 at 17:30
  • @richtigga Oups, that (the `--version` rejection) puts me out of my comfort zone, but the `+%s` looks about right (`GMT: Thursday 14 April 2022 17:29:54`). – Ted Lyngmo Apr 14 '22 at 17:32

0 Answers0