2012-07-31 13:10 阅读 58

从exec.Command调用“ sed”

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()

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条回答 默认 最新

  • 已采纳
    doubi9531 doubi9531 2012-07-31 13:30

    I believe the following works:

    command := exec.Command("sed", "-e","s/hello/goodbye/g","myfile.txt")
    点赞 评论 复制链接分享