Artificial intelligent assistant

How to replace empty lines followed by a specific string by this string? With `sed` or `awk` or whatever, how to replace empty lines followed by a specific string (for instance `&` or `\end{align}`) by this string (hence `&` or `\end{align}`)? As an example, here is the initial file ( **edit** : less ambiguous example): The quick brown fox jumps over the sleazy dog. Indeed, the quick brown fox jumps over the sleazy dog. \begin{align} & foo & bar \end{align} The quick brown fox jumps over the sleazy dog. Indeed, the quick brown fox jumps over the sleazy dog. and here is what I'd like to get: The quick brown fox jumps over the sleazy dog. Indeed, the quick brown fox jumps over the sleazy dog. \begin{align} & foo & bar \end{align} The quick brown fox jumps over the sleazy dog. Indeed, the quick brown fox jumps over the sleazy dog.

`GNU sed` with extended regex support (-E) to aid in regex writing.


sed -Ei -e '
/./b
:a
$q;N
/\
$/ba
s/^\
+(&|\\(begin|end)\{align\})/\1/
' file


Idea is to start collecting empty lines and stop when a nonempty line is seen. Then the regex will check whether the chunk of empty lines is followed by any one of the following three lines:

* a line starting with an ampersand `&`
* a line starting with \begin{align}
* a line starting with \end{align}



Then we delete these specific empty lines.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 2b63b0f28fd92215239a557249d12c07