【问题标题】:Trying to translate bash statement to subprocess in python试图将bash语句翻译成python中的子进程
【发布时间】: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


【解决方案1】:

你有两个问题。首先是您没有正确地将参数转换为sort。当你运行这个sort 命令时:

sort -t' ' -k4 -n -r

shell 将标记 -t' ' 粘贴到单个参数 "-t "(破折号、三通、空格)中。因此,它的正确子流程参数应该是:

sort = subprocess.Popen(['sort', '-t ', '-k4', '-n', '-r'],
                        stdin=awk.stdout,
                        stdout=subprocess.PIPE,
                        )

第二个问题是最终重定向到带有&gt; output.csv 标记的文件。当 shell 看到这一点时,它不会运行名为 &gt; 的命令;相反,它打开文件output.csv 进行写入并将其设置为最后一个命令的标准输出句柄。因此,您不应该尝试将名为&gt; 的命令作为子进程运行;您需要通过打开文件来模拟 shell:

head = subprocess.Popen(['head', '-10'],
                        stdin=sort.stdout,
                        stdout=open('output.csv', 'w'),  # Not a pipe here
                        )

【讨论】:

  • 效果很好。我知道我对 .csv 的输出被抬高了。谢谢!
  • 我还有一个问题。在我的第一行中,我试图搜索包含今天当前日期的文件。 bash 语句使用 LogFile_'date +%Y%m%d'.msg.log (LogFile_20120410.msg.log) 工作。但这在子流程中不起作用。我得到“没有这样的文件或目录”。任何想法为什么这不会在子流程中正确翻译?
【解决方案2】:

你可以重写:

cat LogFile.msg.log | grep ABCD | awk '{print $14,$10,$5,$7}' |
sort -t' ' -k4 -n -r | head -10 > output.csv

在纯 Python 中:

from heapq import nlargest
from operator import itemgetter

select_items = itemgetter(13, 9, 4, 6) # note: zero-based indices
with open('LogFile.msg.log') as file, open('output.csv', 'w') as outfile:
    rows = (select_items(line.split()) for line in file if 'ABCD' in line)
    top10_rows = nlargest(10, rows, key=lambda row: int(row[3]))
    print("\n".join(map(" ".join, top10_rows)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-30
    • 2014-04-25
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    相关资源
    最近更新 更多