1

I know there is tons of material on this online but I could not solve my problem with the information found, hence this question.

This is what I have in my bash script:

value=$(cat <<-EOT
{
  "file_path": "/text1/text2/logs/example.log",
  "log_group_name": "/text3/text4/example.log",
  "log_stream_name": "{hostname}"
},

EOT
)

#value='sed sucks'
#echo $value
sed -i "s#app_log_files#${value}#" ex1.json

ex1.json has code like this

"collect_list": [
                  app_log_files
                  {
                    "file_path": "/text5/text6/messages",
                    "log_group_name":"/text7/text8/messages",
                    "log_stream_name":"{hostname}"
                  },

I think the problem is with ${value} having space, forward slash, comma, quotes. I tried using different delimiters and that did not work. Strangely I am seeing "sed: -e expression #1, char 17: unterminated `s' command" error even though I have closing delimiter in my sed.

Is sed right thing for usage here? Any suggestions or ideas on where I am going wrong. Thanks for help in advance.

sri_84
  • 81
  • 6
  • @WiktorStribiżew thanks for response, but in my case would I have to escape all the characters in ${value}, is there a simpler solution? – sri_84 Oct 12 '18 at 12:52
  • thanks @WiktorStribiżew This fixed it, although I am trying to make sense of it value_esc=$(sed -e '$!a\'$'\n''\\n' <<<"$value" | tr -d '\n') sed -i "s#app_log_files#${value_esc}#" ex1.json – sri_84 Oct 12 '18 at 13:16
  • perl is the best tool for that.. this command does the job perl -e ' BEGIN{$x=qx(cat ex1.json);$y=$ENV{"value"};$z=quotemeta $y;$x=~s/$z//gosm; print "$x" ; exit } ' – stack0114106 Oct 12 '18 at 13:30

0 Answers0