jeremieca
jeremieca

Reputation: 1188

Replace a line by the content of another file

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.

Upvotes: 3

Views: 1650

Answers (2)

clt60
clt60

Reputation: 63974

This can work too, not very effective - but not sensitive to content.

cat <(sed '/#Content/,$d' < FILE ) content.xml <(sed '1,/#Content/d' < FILE) > NEWFILE

so, catenate

  • the first part of FILE (up to #Content - (not included))
  • the file `content.xml
  • the second part of file (from to #Content - (not included))

Upvotes: 1

fedorqui
fedorqui

Reputation: 290525

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.

Upvotes: 3

Related Questions