0

I'm trying to add the following line to the beginning of file using sed command:

node /^$server$/ inherits default {

Command: sed -i "1i node /^$server$/ inherits default {" "file.txt"

When I run the command locally from the server it's working for me, but when I try to run it from bash script using this command:

sudo ssh serverName sed -i "1i node /^$server$/ inherits default {" "file.txt"

I got this error message:

"sed: -e expression #1, char 2: expected \ after a',c' or `i'"

I added \ after the 1i but this just adding the word "node" to the beginning of the file.

The first line of the file need to be for example:

node /^serverName$/ inherits default {

I guess that I need to use regex in my command but I don't know how to do it.

Please assist

Khalil Jomaa
  • 135
  • 1
  • 2
  • 14
  • @TomFenech Yeah, the symtoms suggest OS X. – tripleee Mar 24 '15 at 19:34
  • I tried that but still just adding the word "node". any idea ? – Khalil Jomaa Mar 24 '15 at 19:53
  • I don't think so; if it were BSD sed, `-i` would take a parameter, and `1i foo` would not be interpreted as sed code. To me it looks like a shell quoting issue; probably the command passed to ssh arrives in the other shell in pieces, as though the sed code were not quoted at all. EDIT: Does it work with `ssh serverName "sed -i '1i node /^$server\$/ inherits default {' file.txt"`? – Wintermute Mar 24 '15 at 19:53

0 Answers0