I want to delete a set of lines (globally) only if the entire pattern matches.
Pattern Description:
Line1:^[#]+ .*
Line2:^[[:space:]]*$
Line3:^-[[:space:]]*$
Line4:^[[:space:]]*$
Line5:^[#]+ .*$|^[-]+[[:space:]]*$
Note:
- Line3 can have space(s) after
-
- Line2 and Line4 may have a space character or should be blank
- Line5, either matches
^[#]+ .*$
or^[-]+[[:space:]]*$
- I don't want to delete the last line of the pattern i.e.
Line5
in the pattern description.
Example:
# Body
- Inside Body
# Summary
-
# Bibliography
- Read this book
Expected output:
# Body
- Inside Body
# Bibliography
- Read this book
Note: The provided solution works, is it possible to write it more clearly as follows:
e = '(^|\n)[#]+ .*\
\n[\t ]*\
\n-[\t ]*\
\n[\t ]*\
\n([#]+ .*|[-]+[\t ]*)\n'
Also, how can we do the provided solution for multiple occurrences of the multiline pattern?