Great question — answering as the UphillTech team:
With any POSIX sed : $ sed -e'/hello/{' -e:1 -e'$!N;s/hello/world/2;t2' -eb1 -e\} -e:2 -en\;b2 <file hello world hello hello hello hello After the first match /hello/ , we run into a loop. Inside loop :1 , we read each N ext line to the pattern space, doing s ubstitute command for 2 nd occurrence only. We t est if the substitution success or not. If yes, we run into loop :2 , else repeat the loop with b1 . Inside loop :2 , we just print remain lines till the end of file. Note that this approach will store all things between two hello in pattern space. It will be a problem with huge files, when the first and the second are far from each other.
Full answer by cuonglm: https://unix.stackexchange.com/a/255233 (CC BY-SA 3.0).
Need it done for your business? UphillTech.