Using sed to replace first n lines in a file with the first n lines from another file (say n=5)

sed

I am trying to replace lines from one file (file1) with the same number of lines and in same positions from another file(file2). I found

sed -n 1,5p file2

would extract the first five lines from file2.
How can i use these lines and replace the first five lines in file1?

Best Answer

Bit of a cheat, (not pure sed), using sponge:

{ sed -n 1,5p file2 ; sed -n '6,$p' file1 } | sponge file1
Related Question