In plain awk:
#!/usr/bin/awk
BEGIN {
inside_empty_section = "false"
buffer = ""
}
/^$/ { # Add the empty line to the current section buffer or print it
if (inside_empty_section == "true") { buffer = buffer $0 "\n" } else { print $0 }; next }
/^###/ {
# This is the beginning of a new section.
# Either the previous one was empty: just forget its buffer.
# either it was not empty and has already been printed.
# In any case, just start buffering a new empty section.
inside_empty_section = "true"
buffer = $0
next
}
{
# Found a non-empty line: the current section is NOT empty.
# If it was supposed to be empty, print its buffer.
if (inside_empty_section == "true") { print buffer }
inside_empty_section = "false"
print $0
next
}
Launch it with:
$ awk -f ./test.awk < plop
## Version
### Added
- something
### Changed
- something
Of course, you could make it more concise:
awk '/^$/ {if (i) {b=b $0 "\n"} else {print $0 }; next} \
/^###/ {i=1; b=$0; next} {if (i) {print b}; i=0; print $0; next}' < plop