lukascbossert
lukascbossert

Reputation: 411

Calculate a date by the amount of months passed by in bash

I have list with two items, title and months.

MS4.2.8|48, 60 
MS4.2.9|60 
MS4.2.10|52 
MS4.3.1|3 
MS4.3.2|12 
MS4.3.3|9 
MS4.3.4|12 
MS4.3.5|24,36,48,60 
MS4.3.6|24,36,48,60 
MS4.3.7|24 
MS4.3.8|18 

The start period is "2021-09-01". I need a list with title and date calculated on the amount of months passed by since the start period. The month period can be set multiple times, for each month-period there should be a new line.

For example:

MS4.2.8|2025-08-01
MS4.2.8|2026-08-01 
MS4.2.9|2026-08-01 
MS4.2.10|2026-01-01 
MS4.3.1|2021-11-01 
MS4.3.2|2022-08-01 
MS4.3.3|2022-05-01 
.
.
.

Upvotes: 1

Views: 140

Answers (1)

kvantour
kvantour

Reputation: 26471

This should do it:

awk' BEGIN{OFS=FS="|"}
     {n=split($2,a,",");
      for(i=1;i<=n;++i) print $1,strftime("%F",mktime("2021 " (08+a[i]) " 01 0 0 0"))
     }' file

the method makes use of GNU awk extensions mktime and strftime.

Upvotes: 1

Related Questions