将"sed"称为"sed"从exec.Command [英] Calling "sed" from exec.Command

查看:67
本文介绍了将"sed"称为"sed"从exec.Command的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在尝试运行此代码时遇到麻烦,该代码应该调用Unix命令 sed 来查找字符串 hello 并将其替换为 goodbye 在文件 ./myfile.txt

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

如果您从命令行运行它,那么效果很好,但是如果我从Go代码中尝试了同样的事情....

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?

任何帮助将不胜感激

推荐答案

我相信以下作品可以实现:

I believe the following works:

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

这篇关于将"sed"称为"sed"从exec.Command的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆