Sample Header Ad - 728x90

sed regexp address range minus (or plus) number of lines

4 votes
2 answers
1727 views
I have multiple ICS files that contain something like:
...
PRIORITY:4
DESCRIPTION:this
  was
  a
  triumph
COMPLETED:20180101T160000
...
I am in interested in addressing the DESCRIPTION key. The key DESCRIPTION can occur anywhere in the ICS file. It is _not_ always followed by the key COMPLETED. Using ed, I would do it like this:
/DESCRIPTION/;/^[^ ]/-1p
which results in:
DESCRIPTION:this
  was
  a
  triumph
However, sed does not seem to have that capability:
sed -n '/DESCRIPTION/,/^[^ ]/-1p' filename
results in > sed: -e expression #1, char 22: unknown command: `-' Is there a way to do what I want using sed? ## Solving this particular problem This problem can be solved by:
sed -n '/DESCRIPTION:/,/^\S/ { /\(DESCRIPTION\|^\s\)/p }' example.ics
But this feels very unwieldy and verbose. awk also does not seem to support /begpat/,/endpat/-1, so you would end up with something like:
BEGIN { endpat = "^(DESCRIPTION|\\s)" }
/^DESCRIPTION/, $0 !~ endpat {
    if ($0 ~ endpat) {print}
}
Asked by Stefan van den Akker (352 rep)
Sep 17, 2019, 12:21 PM
Last activity: Feb 23, 2025, 08:13 AM