Llamando a "sed" desde exec.Command

I'm currently having trouble trying to run this code which is supposed to call the unix command sed to find and replace the string hello con goodbye en el archivo ./myfile.txt

This works fine if you run it from the command line, but if I try the same thing from my Go code....

command := exec.Command("sed", "-e \"s/hello/goodbye/g\" ./myfile.txt")
result,err := command.CombinedOutput()
fmt.Println(string(result))

Bit I just keep on getting this output

sed: -e expression #1, char 2: unknown command: `"'

Is there some sort of quote escaping going on or something to cause it to interpret the string wrong?

Cualquier ayuda sería apreciada

preguntado el 31 de julio de 12 a las 14:07

1 Respuestas

Creo que las siguientes obras:

command := exec.Command("sed", "-e","s/hello/goodbye/g","myfile.txt")

Respondido 31 Jul 12, 14:07

Nota: exec.Command("cmd", "a1 a2", "a3") se ejecuta como cmd "a1 a2" "a3" - which is why it didn't work. - Zippo

How can I add the string if it was a variable? e.g. instead of hard-coding "goodbye", if my string was s := "goodbye" , ¿cómo agregar? - Saty

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.