Linux – Replaces a line with the contents of another file

Replaces a line with the contents of another file… here is a solution to the problem.

Replaces a line with the contents of another file

I have a question. I want to replace a line of one file with the contents of another file.

In my first file, I have this line: "#Content", which I want to replace with the contents of the file content.xml.

Thank you.

Solution

This works :

your_new_text=$(cat content.xml | sed 's/[^-A-Za-z0-9_]/\\&/g')
sed -i "s/#Content/$your_new_text/" your_file

It puts the text in content.xml into the variable $your_new_text. Then sed does the job: -i stands for replacement in the file, looks for #Content and replaces with text in $your_new_text.

Note that it must be wrapped with " Parcel to work.

Related Problems and Solutions