drflkphi675447 2012-07-31 13:10
浏览 128
已采纳

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

  • doubi9531 2012-07-31 13:30
    关注

    I believe the following works:

    command := exec.Command("sed", "-e","s/hello/goodbye/g","myfile.txt")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 软件自定义无线电该怎样使用
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下