0

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

:45,50d
: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.

2
  • Have you looked at using sed?
    – MaQleod
    Commented Mar 29, 2012 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
    Commented Mar 29, 2012 at 19:57

1 Answer 1

1
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: http://manpages.debian.net/cgi-bin/man.cgi?query=sed

1
  • 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
    Commented Mar 29, 2012 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .