Calling “sed” from exec.Command

倾然丶 夕夏残阳落幕 提交于 2021-01-28 17:45:00

问题


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 with goodbye in the file ./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?

Any help would be appreciated


回答1:


I believe the following works:

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


来源:https://stackoverflow.com/questions/11740887/calling-sed-from-exec-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!