11

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
  • 34,080
  • 14
  • 96
  • 122
iwan
  • 6,759
  • 15
  • 47
  • 63
  • 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

15

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
  • 56,235
  • 11
  • 119
  • 111
  • 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,732
  • 5
  • 23
  • 44
annonymous
  • 51
  • 1
  • 1