I'm trying to replace VEVENT
to VTODO
entries in an .ics file if it matches current date
on another line (it was exported incorrectly):
BEGIN:VCALENDAR
BEGIN:VEVENT
DTSTART:20220340T140000
END:VEVENT
BEGIN:VEVENT
DTSTART:20230620T193700
END:VEVENT
BEGIN:VEVENT
DTSTART:20210210T193800
END:VEVENT
END:VCALENDAR
The second VEVENT entry has current time so it should become:
BEGIN:VTODO
DTSTART:20230620T193700
END:VTODO
There are more entries between BEGIN:VEVENT
and END:VEVENT
lines, I've redacted them for clarity.
I've tried this with sed, but the ranges pick the first occurrence of VEVENT in the entire file, not first occurrence after (or before) the matched pattern, so it replaces all of them.
sed -i "/BEGIN:VEVENT/,/DTSTART:$(date +%Y%m%dT%H%M)/{s/VEVENT/VTODO/}" org.ics
I was trying to adapt it to another question here, which I thought was relevant: Find a string and replace another string after the first is found
sed -n "/DTSTART:$(date +%Y%m%dT%H%M)/,${/END:VEVENT/{x//{x b}g s/VEVENT/VTODO/}}" org.ics
but it didn't work at all:
sed: -e expression #1, char 25: unexpected
,'`
sed 'H;/BEGIN:VEVENT/h;/END:VEVENT/!d;x;/DTSTART:20230620T193700/s/VEVENT/VTODO/g' org.ics
?sed 'H;/BEGIN:VEVENT/h;/END:VEVENT/!d;x;/DTSTART:'"$(date +%Y%m%dT%H%M)"'/s/VEVENT/VTODO/g' org.ics
sed wizardry
!There are more entries between BEGIN:VEVENT and END:VEVENT lines, I've redacted them for clarity.
- if any of those might contain BEGIN, END, VEVENT, DSTART, etc. as [sub]strings (e.g. maybe there's some kind of description field about the event that might say things including typos like "DESCRIPTION:This is the best VEVENT we've had. In the BEGINNING we could DSTART to open presents but it ENDs too soon") then you should include that in your sample input/output as most, if not all, of the posted solutions will fail given some of those strings being present in different locations.