Double match - substitute pattern on subsequent line if prevous line matches another pattern?
1
vote
4
answers
167
views
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: https://unix.stackexchange.com/questions/418946/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
,'`
Asked by Daniel Krajnik
(371 rep)
Jun 20, 2023, 06:50 PM
Last activity: Jun 21, 2023, 02:25 PM
Last activity: Jun 21, 2023, 02:25 PM