For some problems like matching a pattern over an unknown number of lines or "replace the last occurence of ..." the option -z of GNU sed is really helpful. How can I achieve the same thing portable?
Example: I have a file
yellow, green,
blue, black, purple,
orange,
white, red, brown
are some colours
and I want to replace the last comma of the file with and. Note that it is unknown in which line or where in that line the comma is. With GNU sed I can do
sed -z 's/\(.*\),/ \1 and/'
to get the desired output
yellow, green,
blue, black, purple,
orange,
white, red and brown
are some colours
How can I do it in a portable way, that will run with any POSIX sed?