I know how to replace a line with another line or several other lines with tools like sed
. But is there an easy way, to replace a line in a file with the whole content of a second file?
So, let's have an example. I have a file called file1.txt
:
A 1
B 2
C 3
And I have a second file file2.txt
:
line 1
line 2
line 3
Now, I want to replace line 2
with the whole content of file1.txt
, so in the end, it should look like this
line 1
A 1
B 2
C 3
line 3
One way I could think about would be something like this:
sed -i "s/line 2/$(cat file1.txt)/g" file2.txt.
But then I also have to check some special characters like /
and maybe more. I have to assume, that every possible readable character could be in file1.txt
.
So, back to my question: Is there an easy way, to replace a line in a file with the whole content of a second file? It doesn't have to be sed
. It could be also another tool, if it could do the job better...