Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a line by the content of another file

Tags:

linux

bash

shell

I have a problem. I would like replace a line of a file by the content of another file.

In my first file I have this line: "#Content" and I would like to replace that with the content of the file content.xml.

Thanks.

like image 288
jeremieca Avatar asked Sep 15 '25 10:09

jeremieca


1 Answers

This can work:

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 from content.xml in the variable $your_new_text. Then sed does the work: -i stands for replacing in the file, looks for #Content and replaces by text inside $your_new_text.

Note that it has to be wrapped with " to work.

like image 98
fedorqui 'SO stop harming' Avatar answered Sep 17 '25 00:09

fedorqui 'SO stop harming'