2014-09-30 3 views
0

아래 명령은 bash에서 정상적으로 실행됩니다.bash in python : sed unterminated s command

/folk/vlm/commandline/./vlmTool find -l all | grep -e "^Target ID" -e "City" -e "Zone" | sed "s#.*City.*#&\n#g" > new.txt 

는하지만 파이썬에서 내가 얻을 같은 명령을 실행하려고하면

['sed', 's#.*City.*#&\n#g'] 
**sed: -e expression #1, char 12: unterminated `s' command** 

코드 :

\n 시퀀스는 대한 이스케이프 시퀀스로 파이썬으로 이해된다
#!/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] 
+1

'[sed', r 's #. * City. * # & \ n # g ']'시도하십시오. – SethMMorton

답변

2

개행; 그래서 sed는 s 명령으로 끝내고 새 명령을 시작했다고 생각합니다. 문자열에 백 슬래시를 포함 시키려면 ...\\n...에서와 같이 이스케이프 처리하거나 원시 문자열을 사용하십시오. r's#.*City.*#&\n#g'

+0

감사합니다 ... \\ n 원하는 출력으로 작업했지만 r을 사용하면 원하는 결과를 얻을 수 없습니다. – Punit

+0

당신이 최고입니다. –