'Ed' and a 'here' document will do what you need. E.g: for i in file1 file2 ...... do ed $i << THE_END g/RE_1/.,/RE_2/c\\ new text, with each new line escaped w . THE_END done Where RE_1 (RE_2) matches the first (last) line of the text that must be replaced.