Splitting week start date and end date based on custom period start dates

Below are my custom period start and end dates based on a calender, these dates are placed in a file, for each period i need to split into three weeks for each period row, example is given below.

Could you please help out to achieve solution through shell script..

 File content:

period_start_date	period_end_date	period_code
2020-01-01	2020-01-15	P1
2020-01-05	2020-01-19	P2
2020-01-16	2020-01-31	P1
2020-01-20	2020-02-04	P2
2020-02-01	2020-02-15	P1
2020-02-05	2020-02-19	P2
2020-02-16	2020-02-29	P1
2020-02-20	2020-03-04	P2
2020-03-01	2020-03-15	P1
2020-03-05	2020-03-19	P2
2020-03-16	2020-03-31	P1
2020-03-20	2020-04-04	P2
2020-04-01	2020-04-15	P1
2020-04-05	2020-04-19	P2
2020-04-16	2020-04-30	P1
2020-04-20	2020-05-04	P2
2020-05-01	2020-05-15	P1
2020-05-05	2020-05-19	P2
2020-05-16	2020-05-31	P1
2020-05-20	2020-06-04	P2
Expected Result for  					
1) period start date 	2020-01-01	Period End date	2020-01-15	Period code 	 P1        
2) period start date 	2020-01-05	Period End date	2020-01-19	Period code 	 P2     
3) period start date 	2020-01-16	Period End date	2020-01-31	Period code 	 P1     
4) period start date 	2020-01-20	Period End date	2020-02-04	Period code 	 P2   
week Start Date	Week End Date	Period strat Date	Period end Date	Period code
2020-01-01	2020-01-05	2020-01-01	2020-01-15	P1
2020-01-06	2020-01-12	2020-01-01	2020-01-15	P1
2020-01-13	2020-01-15	2020-01-01	2020-01-15	P1
2020-01-05	2020-01-05	2020-01-05	2020-01-19	P2
2020-01-06	2020-01-12	2020-01-05	2020-01-19	P2
2020-01-13	2020-01-19	2020-01-05	2020-01-19	P2
2020-01-16	2020-01-19	2020-01-16	2020-01-31	P1
2020-01-20	2020-01-26	2020-01-16	2020-01-31	P1
2020-01-27	2020-01-31	2020-01-16	2020-01-31	P1
2020-01-20	2020-01-26	2020-01-20	2020-02-04	P2
2020-01-27	2020-02-02	2020-01-20	2020-02-04	P2
2020-02-03	2020-02-04	2020-01-20	2020-02-04	P2

This forum is not a script writing service.

If you have a solution you have worked on that is not complete we can help you, but you must have shown some effort to solve this yourself.

Also because date manipulation in shell script is not very portable, we will need to know the Operating system and shell you are using.

1 Like

Given the input is exactly as given in post #1, i.e. no more nor less than three weeks between the two period dates, try (running awk thrice and date twice only, and opening / reading file twice as well)

awk '
NR > 1 {print $1 ORS $2}
' file |
date -f- +"%s %u" |
awk '
BEGIN   {SECDAY = 86400
         SEC6DY = SECDAY * 6
        }

        {WSD = $1
         WED = WSD + (7 - $2) * SECDAY
         getline
         print "@" WSD
         while (WED < $1)       {print "@" WED
                                 WSD = WED + SECDAY
                                 WED = WSD + SEC6DY
                                 print "@" WSD 
                                }
         print "@" $1
 }' |
date -f- +"%Y-%m-%d" |
awk '
BEGIN           {getline LN < "file"
                 print "week Start Date", "Week End Date", LN
                } 
                
NR%6 == 1       {getline LN < "file"
                } 
                {getline TMP
                 print $0, TMP, LN
                }
' OFS="\t" 
week Start Date Week End Date period_start_date    period_end_date    period_code
2020-01-01    2020-01-05    2020-01-01    2020-01-15    P1
2020-01-06    2020-01-12    2020-01-01    2020-01-15    P1
2020-01-13    2020-01-15    2020-01-01    2020-01-15    P1
2020-01-05    2020-01-05    2020-01-05    2020-01-19    P2
2020-01-06    2020-01-12    2020-01-05    2020-01-19    P2
2020-01-13    2020-01-19    2020-01-05    2020-01-19    P2
2020-01-16    2020-01-19    2020-01-16    2020-01-31    P1
2020-01-20    2020-01-26    2020-01-16    2020-01-31    P1
2020-01-27    2020-01-31    2020-01-16    2020-01-31    P1
.
.
 .

The first awk prints periods' start and end alternately, then date converts that entire input stream to epoch seconds and day of week line by line, then awk produces three weeks' start and end dates totaling in 6 epoch seconds per input line, then date converts those back to the desired date format, and awk merges these with the original input file.