0

I am passing to sed for replacement text that seems to have some characters that it does not like.
The text comes from git log graph and is something like:

ID- desc author                                                                                                   
ID- desc author  

I get unescaped newline inside substitute pattern
How can I escape everything before piping to sed?

Example:    
COMMIT=$(git log my_branch...origin/master --pretty=format:'%h %an')    
FINAL=$(cat msg.txt | sed -E "s/--PLACEHOLDER--/$COMMIT/)    
Jim
  • 1,391

1 Answers1

1

You can do it with plain bash parameter substitution:

msg=$(< msg.txt)
# or, for this demo
msg="This is the commit message.
--PLACEHOLDER--
That's it."

commit="id1 - message 1
id2 - message 2
id3 - message 3"

final="${msg//--PLACEHOLDER--/"$commit"}"
echo "$final"
This is the commit message.
id1 - message 1
id2 - message 2
id3 - message 3
That's it.
glenn jackman
  • 85,964
  • This works but what's up with the double quote around "$commit"? Works without that – Jim Jun 11 '18 at 15:26