14

I run below sed command

sed -i s/abc=.*$/abc=def ghi/g hpq_sf_attach_wf_param.txt

and it gave me error:

sed: -e expression #1, char 17: unterminated `s' command

I noticed it is due to space in between of def and ghi. Any idea how to fix it?

codeforester
  • 39,467
  • 16
  • 112
  • 140
iwan
  • 7,269
  • 18
  • 48
  • 66
  • Possible duplicate of [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Feb 13 '18 at 08:43
  • Possible duplicate of [Error with sed unterminated s command in bash](https://stackoverflow.com/questions/31277018/error-with-sed-unterminated-s-command-in-bash) – tripleee Nov 22 '18 at 12:26

2 Answers2

16

You need to use quoting to protect special characters, including spaces, $, and *.

sed -i 's/abc=.*$/abc=def ghi/g' hpq_sf_attach_wf_param.txt
geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • hi , adding single quote caused the update to failed, nothing is changing within the target file hpq_sf_attach_wf_param.txt ~ although it should have been updated – iwan Apr 25 '12 at 04:30
  • Are you sure your pattern is correct then? Perhaps edit your question to include some sample lines from `hpq_sf_attach_param.txt`? – geekosaur Apr 25 '12 at 04:37
5

So geekosaur had it right. The the reason you had the problem though is because it needs to be double quotes for the wildcards because with single quotes it takes them as literal characters, not for the meaning you want.

sed -i "s/abc=.*$/abc=def ghi/g" hpq_sf_attach_wf_param.txt

Also if the space between "def" and "ghi" gives you problems, adding a "\" should help making it read it as a literal space.

sed -i "s/abc=.*$/abc=def\ ghi/g" hpq_sf_attach_wf_param.txt
James Jones
  • 3,850
  • 5
  • 25
  • 44
annonymous
  • 59
  • 1
  • 1
  • This explanation makes absolutely no sense: "it needs to be double quotes for the wildcards because with single quotes it takes them as literal characters". The only relevant shell metacharacter in the string is `$`, and `$/` is not a valid shell variable so is left untouched. Consider: `echo "s/abc=.*$/abc=def ghi/g"|md5sum; echo 's/abc=.*$/abc=def ghi/g'|md5sum`. If there was a difference, why is md5sum output identical? Also, `\ ` does absolutely nothing useful. – jhnc Jan 25 '23 at 21:38
  • can someone suggest me what is going wrong here - sed -i "s|##CONNECTION_DETAILS##|${CONNECTION_DETAILS}|g" dep.yaml. The value of CONNECTION_DETAILS is in encoded form CSAgewogICAgICAgICJjb25uZWN0aW9uX25hbWVfMSI6ICJPRlNBQV9BTkFMWVRJQ1NfUFVCIiwK ICAgICAgICAiZGF0YWJhc2VfbmFtZV8xIjogIk9GU0FBX0FOQUxZVElDU19QVUIiLAogICAgICAg ICJiaV9TZXJ2aWNlc18xIjpbImR2IiwicnBkIiwiYmlwdWJsaXNoZXIiXQogICAgICB9Cg== – Meenal Aug 19 '23 at 11:45