Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with my string substitution using sed on Mac OS X?

Tags:

macos

sed

I want to replace #Banner none with Banner /etc/sshd_banner that is within /etc/sshd_config. If I run

sudo sed -i "s/#Banner none/Banner \/etc\/sshd_banner" /etc/sshd_config

I get the following error

sed: 1: "/etc/sshd_config": unterminated substitute pattern

Any ideas on how to fix this issue?

like image 552
Technic1an Avatar asked Feb 18 '15 19:02

Technic1an


2 Answers

Three problems with your command:

  1. You're missing the terminating /.
  2. You can't use / as delimiter anyway, because this character occurs in the string you're trying to replace/substitute. You should use a different character, such as a pipe character, as delimiter.
  3. In the version (BSD) of sed that ships with Mac OS X, the -i flag expects a mandatory <extension> argument, which your command is missing. An empty string ("") should follow the -i flag if you want to edit the file in-place with this version of sed.

In summary, try

sudo sed -i "" "s|#Banner none|Banner /etc/sshd_banner|" /etc/sshd_config 
like image 179
jub0bs Avatar answered Sep 17 '22 13:09

jub0bs


Use another delimiter

Eks here I do use " as delimiter

sudo sed -i "" "s|#Banner none|Banner /etc/sshd_banner|" /etc/sshd_config

By changing the delimiter, you do not need to escape the /

Your original post missed one / at the end.

From OS X manual

-i extension
         Edit files in-place, saving backups with the specified extension.  If a zero-length extension
         is given, no backup will be saved.  It is not recommended to give a zero-length extension when
         in-place editing files, as you risk corruption or partial content in situations where disk
         space is exhausted, etc.

zero-length = ""

like image 36
Jotne Avatar answered Sep 18 '22 13:09

Jotne