Wyatt Gillette Wyatt Gillette - 23 days ago 8
Linux Question

Append a file in the middle of another file in bash

I need to append a file in a specific location of another file.
I got the line number so, my file is:

file1.txt



I

am

Cookie


While the second one is

file2.txt



a

black

dog

named


so, after the solution, file1.txt should be like


I

am

a

black

dog

named

Cookie


The solution should be compatible with the presence of characters like " and / in both files.

Any tool is ok as long as it's native (I mean, no new software installation)

Thanks!

Answer Source

Could you please try following and let me know if this helps you.

awk 'FNR==3{system("cat file2.txt")} 1' file1.txt

Output will be as follows.

I
am
a
black
dog
named
Cookie