下面的命令在bash中正常運行。python中的bash:sed unterminated s命令
/folk/vlm/commandline/./vlmTool find -l all | grep -e "^Target ID" -e "City" -e "Zone" | sed "s#.*City.*#&\n#g" > new.txt
但是在Python當我嘗試執行相同的命令,我得到:
['sed', 's#.*City.*#&\n#g']
**sed: -e expression #1, char 12: unterminated `s' command**
代碼:
#!/usr/local/bin/python -tt
import subprocess
import shlex
cmd = '/folk/vlm/commandline/./vlmTool find -l all | grep -e "^Target ID" -e "City" -e "Zone" | sed "s#.*City.*#&\n#g" > new.txt'
cmd2= "/folk/vlm/commandline/./vlmTool find -l all"
args1 = shlex.split(cmd2)
cmd3 = 'grep -e "^Target ID" -e "City" -e "Zone"'
args2 = shlex.split(cmd3)
cmd4 = 'sed "s#.*City.*#&\n#g"'
args3 = shlex.split(cmd4)
print args3
p1 = subprocess.Popen(args1, stdout=subprocess.PIPE)
p2 = subprocess.Popen(args2, stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen(args3, stdin=p2.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
p2.stdout.close()
output = p3.communicate()[0]
嘗試'['sed',r's#。* City。*#&#n#g']''。 – SethMMorton 2014-09-30 17:25:38