Bash copy file contents into an existing file at a specific location

Hi all

I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far:

File A
line 1
line 2
line 3
line 4

File B
paragraph 1
paragraph 2

So I would end up with:
File A
line 1
line 2
paragraph 1
paragraph 2
line 3
line 4

Here's what I have been trying to do:

while read line
do
sed '$POSa\$line' B.txt > A.txt
done < B.txt

Thanks for the help.

sed '2 r file2' <file1
line 1
line 2
paragraph 1
paragraph 2
line 3
line 4

I tried that and I get the following error:

sed: -e expression #1, char 3: extra characters after command

Here's the line I tried:

sed '$POS r ${PACKAGE}.xml' < a_Overview2_New.xml

My line works, yours doesn't :wink: :smiley:
Inside single quotes no substituion can occure.

Try

sed "$POS r ${PACKAGE}.xml" < a_Overview2_New.xml 

My bad on the quotes... When I run it, it does what I expect, however my a_Overview2_New.xml file is now empty. I want that file to contain:

line 1
line 2
para 1
para 2
line 3
line 4

You cannot read a file and overwrite it in instantly. Just redirect output to a temporary file with ">" and just rename it to the original with "mv".

Okay...thanks for the help. That's what I ended up starting to do.