我想下面执行系统命令使用python:使用下面的Python代码Python的不一致标准输出subprocess.popen
cat txt_file | egrep "keyword1|keyword2|keyword3"
:
p1 = subprocess.Popen (['cat', txt_file], stdout=subprocess.PIPE)
p2 = subprocess.Popen (['egrep', "\"" + keyword_list + "\""], stdin=p1.stdout, stdout=subprocess.PIPE)
#where keyword_list is : "keyword1|keyword2|keyword3"
p1.stdout.close() #for p2 to exit if SIGPIPE from p1
out = p2.communicate()[0]
有多种线路的egrep的输出,但是使用上面的脚本,我只能得到与变量out
中的中间关键字2匹配的行。
这里有什么问题?
更新: 平台:窗口 txt_file相当大〜8 MB