2

I want to write a sed (or awk) command to replace a string from one file with the entire contents of another file. Note that second file from which I want to get the content has more than one line. I tried this:

sed -e "s/PLACEHOLDER/$(sed 's:/:\\/:g' TestOutput.txt)/" SQLInput.txt

but got an error saying sed: -e expression #1, char 22: unterminated 's' command

dhag
  • 15,736
  • 4
  • 55
  • 65

2 Answers2

5

try

sed -i '/PLACEHOLDER/ r TestOutput.txt' SQLInput.txt

where

  • -i edit in place
  • /PLACEHOLDER/ search for pattern
  • r TestOutput.txt read file

note that /PLACEHOLDER/ is not deleted.

to have it deleted

sed -i -e '/PLACEHOLDER/ r TestOutput.txt' -e s/PLACEHOLDER// SQLInput.txt

where

  • -e /PLACEHOLDER/d will delete entire line with PLACEHOLDER
  • -e s/PLACEHOLDER// will delete PLACEHOLDER string
Archemar
  • 31,554
0

A sed code is simpler in this case, but since there's also an awk tag on this question:

awk 'function readfile (file) { getline var < file ; return var }
     BEGIN { RS="^$" }; gsub(/PLACEHOLDER/,readfile("TestOutput.txt"))
' SQLInput.txt
Janis
  • 14,222