2017-06-02 147 views
0

我有linux命令下面的输出:需要解析linux命令的输出

/auto/qalogs/branch_team_5.7/drt/hash_list/bk20170401/audit-gc.rb:11:{:component=>"Encryption", :params=>"-f /auto/qalogs/branch_team/drt/hash_list/enc_options_rkm_ekm.rb log_level=debug", :script=>"encryption/destroy.rb", :timeout=>10800, :skipcheckconnection=>1, :minlimitver=>"5.3.0.0"} 
/auto/qalogs/branch_team_5.7/drt/hash_list/bk20170401/encryption-audit-gc-part1.rb:11:#{:component=>"Encryption", :params=>"-f /auto/qalogs/branch_team/drt/hash_list/enc_options_rkm_ekm.rb log_level=debug", :script=>"encryption/destroy.rb", :timeout=>10800, :skipcheckconnection=>1, :minlimitver=>"5.3.0.0"} 
Binary file /auto/qalogs/branch_team_5.7/ert/hash_list/.encryption-audit-gc.rb.swp matches 
/auto/qalogs/branch_team_5.7/ert/hash_list/encryption-audit-gc.rb:11:{:component=>"Encryption", :params=>"-f /auto/qalogs/branch_team/drt/hash_list/enc_options_rkm_ekm.rb log_level=debug", :script=>"encryption/destroy.rb", :timeout=>7200, :skipcheckconnection=>1, :minlimitver=>"5.3.0.0"} 

我只需要过滤此散列文件:

{:component=>"Encryption", :params=>"-f /auto/qalogs/branch_team/drt/hash_list/enc_options_rkm_ekm.rb log_level=debug", :script=>"encryption/destroy.rb", :timeout=>7200, :skipcheckconnection=>1, :minlimitver=>"5.3.0.0"} 

,并写入到另一个文件。

我试过followng:

out=subprocess.getoutput('grep -rwn 
/auto/qalogs/branch_team_5.7/ert/hash_list/ -e '+alist[0]+'') 
print ("output of grep is", out) 
pattern=re.compile(r'(/auto/qalogs/branch_team_5.7/ert/hash_list/.*.rb:\d) 
(\:)(\{.*\})',out) 
print (pattern.groupobject(0)) 

我得到这个exception.How办呢?

+0

你应该看看're'文件以了解如何使用它 – Morb

+0

为什么你使用'subprocess(grep ...'?为什么不只使用'module re'? – stovfl

+0

我想如果你可以做一点点更清楚你要输入到正则表达式中的输入内容,你想要匹配的字符串,以及你想要获得的组,我能够帮助的人。 –

回答

0

这应该是一个简单的re问题。

import re 

command = 'grep -rwn /auto/qalogs/branch_team_5.7/ert/hash_list/ -e ' + alist[0] + '' 
filter = '/auto/qalogs/branch_team/drt/hash_list/enc_options_rkm_ekm.rb'  

log = subprocess.subprocess.getoutput(command) 

with open('output.txt', 'w+') as f: 
    for message in log: 
     if re.search(filter, message): 
      f.write(result) 

不幸的是,我没有更多的样本数据真正审查它!