【发布时间】:2013-04-10 04:36:00
【问题描述】:
我正在尝试将 bash 命令转换为 python 中的子进程。 bash 命令是:
cat LogFile.msg.log | grep ABCD | awk '{打印 $14,$10,$5,$7}' |排序 -t' ' -k4 -n -r |头 -10 > 输出.csv
到目前为止,我有以下子流程:
cat = subprocess.Popen(['cat', 'LogFile.msg.log'],
stdout=subprocess.PIPE,
)
grep = subprocess.Popen(['grep', 'ABCD'],
stdin=cat.stdout,
stdout=subprocess.PIPE,
)
awk = subprocess.Popen(['awk', '{print $14,$10,$5,$7}'],
stdin=grep.stdout,
stdout=subprocess.PIPE,
)
sort = subprocess.Popen(['sort', '-t','' '', '-k4', '-n', '-r'],
stdin=awk.stdout,
stdout=subprocess.PIPE,
)
head = subprocess.Popen(['head', '-10'],
stdin=sort.stdout,
stdout=subprocess.PIPE,
)
out = subprocess.Popen(['>', 'output.csv'],
stdin=head.stdout,
stdout=subprocess.PIPE,
)
end_of_pipe = out.stdout
现在我收到以下错误:
Sort: empty tab
Traceback (most recent call last):
File "./latency2", line 39, in <module>
stdout=subprocess.PIPE,
File "/usr/lib64/python2.6/subprocess.py", line 639, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我确定我错过了什么,但不确定是什么。
【问题讨论】:
-
你确定你的工作目录是正确的吗?
标签: python subprocess output pipeline export-to-csv