20100119

substitute multiple line pattern in sed

Today I faced a problem with a multiline replacement. I did not want to write a sophisticated regex.
I just wanted to get a copy-pasted block of code and put it in place of the old one. Selected weapon - sed.
I've chosen the /begin/,/end/ matching syntax.

['test' input file]

111
222
333
if (aaa) {
bbbbb
}xxx
}
444
while (0) {
ccccc
}
666


['change' script]

#!/usr/bin/ksh

sed '
/if (aaa)/,/\ }$/ c\
CHANGE\
WAS\
MADE
' \
test


[session]

$ ./change
111
222
333
CHANGE
WAS
MADE
444
while (0) {
ccccc
}
666