Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to replace 5 rows of text in a file with the text of another file in my script, I know that running vim I can do

:r replacement.txt

to delete 5 rows and then insert the entire file, but I want to know if it's possible to do this from a script & how? I've searched high & low and cannot find any clue how to do something like this.

share|improve this question
Have you looked at using sed? – MaQleod Mar 29 '12 at 19:48
@MaQleod I didn't know sed could do multi-line, or replace with text from a file, I've only ever seen it with regex, could you give an example? – JKirchartz Mar 29 '12 at 19:57
up vote 1 down vote accepted
sed -e '45,50d' -e '44r replacement.txt' filename

To save "filename" in-place, use sed -i ...

sed can do lots of things. Here's a man page for it:

share|improve this answer
it took me a couple errors to realize sed -i ... didn't have elipses and use ... as the attribute for -i like sed -i ... -e '45,50d' -e '44r replacement.txt' filename – JKirchartz Mar 29 '12 at 20:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.