我试图在VMkernel中使用sed执行替换。我使用了以下命令,
sed s/myname/sample name/g txt.txt我说sed: unmatched '/'时出错了。我用\代替了空间。啊,真灵。
当我用python做同样的尝试时,
def executeCommand(cmd):
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
print (output.decode("utf-8"))
executeCommand('sed s/myname/sample\ name/g txt.txt')我再次得到错误的sed: unmatched '/'。我使用的是\s,而不是空格,我的名字被samplesname替换了。
如何用空格替换字符串?
发布于 2017-07-14 07:52:55
最简单的做法是不明智地拆分命令:
executeCommand(['sed', 's/myname/sample name/g', 'txt.txt'])否则,您将打开一罐蠕虫,有效地扮演shell解析器角色。
或者,您可以在shell中运行该命令,并让shell解析并运行命令:
import subprocess
def executeCommand(cmd):
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
# Or:
# This will run the command in /bin/bash (instead of /bin/sh)
process = subprocess.Popen(['/bin/bash', '-c', cmd], stdout=subprocess.PIPE)
output, error = process.communicate()
print (output.decode("utf-8"))
executeCommand("sed 's/myname/sample name/g' txt.txt")https://stackoverflow.com/questions/45096181
复制相似问题