1

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

marked as duplicate by Stéphane Chazelas, Anthon, don_crissti, jimmij, jordanm Mar 19 '15 at 16:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

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
  • I want the PLACEHOLDER to be deleted. Whats the change I should make? – Sandeep Dongapure Mar 19 '15 at 14:01
  • Thats deleting entire line containing PLACEHOLDER string. I want to replace only word PLACEHOLDER with contant from second file. Please let me know – Sandeep Dongapure Mar 19 '15 at 14:28
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
  • (assumes GNU awk or mawk for the regexp RS. Most other awk implementations will split on ^ characters there). – Stéphane Chazelas Mar 19 '15 at 14:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.