One can use a command such as the following to substitute the word “before” to the word “after” where “before” occurs between the pair of words “begin” and “end”:
sed ‘/begin/,/end/ {s/before/after/g}’
I am looking for a way to substitute “before” by “after” only if they do not occur inside a pair of “begin” and “end”.
The inversion is almost trivial:
Be aware that given the following input
Your original script gives the output:
Note that the ‘end’ on the same line as the ‘begin’ does not turn off the substitution. (And if you use ‘end before’ after a ‘begin’, the ‘before’ is still substituted by ‘after’.)