Posted By

bubnoff on 09/22/11


Tagged

Bash sed


Versions (?)

Remote sed editing multiple hosts: How to quote


 / Published in: Bash
 

Here's a quick one-off trick for sed editing conf files on remote hosts. I will post the core sed command first, showing the crazy quoting, then a for loop demonstrating how one might tackle multiple hosts. Took me a few tries to get the quoting so thought I'd save others the trouble.

In this example I had to change the homepage variable in a remote conf file.

  • The URL needs single quotes in the conf file. '\'' does the trick.
  • The End of Line metacharacter '$' needs to be escaped for bash. Like so, \$.
  • Finally, note that the sed expression is wrapped in single quotes while the whole command is wrapped with double quotes. I tried other combos -- if you know why this is so, do tell.
  1. ssh root@$host "sed -i 's#\(CONF_VAR=\).*\$#\1'\''http://fakesite.org/splash'\''#' /etc/prog/prog-conf.conf"
  2.  
  3. for host in 10.10.23.{11..33}
  4. do
  5. ssh root@$host "sed -i 's#\(CONF_VAR=\).*\$#\1'\''http://fakesite.org/splash'\''#' /etc/prog/prog-conf.conf"
  6. done

Report this snippet  

You need to login to post a comment.