2013-04-10 42 views
2

我想將bash命令翻譯成python中的子流程。 bash命令是:試圖將bash語句翻譯爲python中的子流程

cat LogFile.msg.log | grep ABCD | awk'{print $ 14,$ 10,$ 5,$ 7}'| sort -t''-k4 -n -r |頭-10> output.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 

我敢肯定,我錯過了一些東西,但不知道是什麼。

+0

你確定你的工作目錄是正確的? – eazar001 2013-04-10 04:57:46

回答

3

你有兩個問題。首先是你沒有正確地將參數翻譯成sort。當運行此命令sort

sort -t' ' -k4 -n -r 

殼糊劑一起令牌-t' '到單個參數"-t "(破折號,三通,空間)。因此,對於它的正確的子進程的參數應該是:

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

第二個問題是最終的重定向與> output.csv標記的文件。當shell看到這個時,它不會運行一個名爲>的命令;相反,它會打開文件output.csv進行寫入,並將其設置爲最後一個命令的標準輸出句柄。所以,你不應該試圖運行一個名爲>的命令作爲子進程;您而是需要通過打開一個文件來模擬殼:

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

完美的工作。我知道我的輸出爲.csv被擡高了。謝謝! – user2234571 2013-04-10 05:36:41

+0

我還有一個問題。在我的第一行中,我試圖搜索包含當前日期的文件。 bash語句使用LogFile_'date +%Y%m%d'.msg.log(LogFile_20120410.msg.log)。但是這在子進程中不起作用。我得到「沒有這樣的文件或目錄」。任何想法,爲什麼這不會在子進程中正確翻譯? – user2234571 2013-04-10 05:46:26

1

您可以重寫:

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)))