Replace string with contents of a file using sed

replacesed

I have two different files:

File1

/home/user1/  
/home/user2/bin  
/home/user1/a/b/c

File2

<TEXT1>
<TEXT2>

I want to replace the <TEXT1> of File2 with the contents of File1 using sed. I tried this command, but not getting proper output:

cat File2|sed "s/<TEXT1>/$(cat File1|sed 's/\//\\\//g'|sed 's/$/\\n/g'|tr -d "\n")/g"

You can use other tools also to solve this problem.

Best Answer

Here's a sed script solution (easier on the eyes than trying to get it into one line on the command line):

/<TEXT1>/ {
  r File1
  d
}

Running it:

$ sed -f script.sed File2
/home/user1/
/home/user2/bin
/home/user1/a/b/c
<TEXT2>
Related Question