appending to sed output of one file into the middle of file

hi,
i have a file

file1 file2
----------- -----------------
aa bbb ccc 111 1111 1111
ddd eee fff 222 3333 4444
ggg hhh iiii 555 6666 777

i select the portion of file1 as

sed -n '/aa/,/fff/p' file1 ,, this select first two lines of file1,

now i need to append these lines to the middle of file2 ie between line1 and line2

Please help me

sed -n '/aa/,/fff/p' file1 > tmp
sed '1 r tmp' file2

head -1 file2
sed -n '/aa/,/fff/p' file1
tail +2 file2

sed "1a\\
$(sed -n -e "/aa/,/fff/{/fff/ ! s/$/\\\\/;p; }" file1)
" file2

hi anbu23
that was a cute answer,,,, :slight_smile:
but wat about the scenario, in which i replace any pattern space by some file contents.... :eek:

You can use change command

sed "1c\\
$(sed -n -e "/aa/,/fff/{/fff/ ! s/$/\\\\/;p; }" file1)
" file2

Replace first line in pattern space by file1 contents