2017-08-29 32 views
1

您好我想从shell脚本移动到Python无法使用子

awk -F' ' '{print FILENAME " "$2 " " $3 " " $5}' $text| tail -n+4|head -n -2

同时使用Python创建子在python脚本来执行awk命令,我得到一个错误。你能否请我哪里错了。我尝试使用转义字符后F.

c1 = subprocess.Popen(["awk -F' ' '{print FILENAME " "$2 " " $3 " " $5}' {0} | tail -n+4|head -n -2".format(text)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
output = c1.communicate() 
print output 
+0

对于'c1',它看起来像你想传递一个字符串而不是一个列表并且使用'shell = True'参数 - 另外,如果你这样做确保你相信'场所'的供应商_完全_因为你很容易受到注射。否则,你需要在python中构建shell管道(虽然稍微笨拙一些,但仍然有可能) – mgilson

+0

我也试过这个c1 = subprocess.Popen(['awk','-F''“,'' {print FILENAME“”$ 2“”$ 3“”$ 5}'“,{0}] .format(text)],stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE) c2 = subprocess。 Popen(['tail','-n + 4'],stdin = c1.stdout,stdout = subprocess.PIPE) c3 = subprocess.Popen(['head','-n','-2'], stdin = c2.stdout,stdout = subprocess.PIPE) 输出,err = c3.communicate() – Amitabh

+0

我试过shell = True,我得到一个错误KeyError:'print FILENAME $ 2 $ 3 $ 5' – Amitabh

回答

0

我能够通过

第一=“AWK -F”'“{打印 “+场地+” $ 2 “+” $ 3 “+” 来解决这个$ 5}'“+ venue1 +”| tail -n + 4 | head -n -2“

c1 = subprocess.Popen([first],shell = True,stdout = subprocess.PIPE,stderr = subprocess.PIPE)

X = c1.communicate()[0]

输出为 xyz 我需要x,y和z之间的空间