需要解析linux命令的输出

时间:2017-06-02 08:14:55

标签: python regex

我有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"} 

并写入另一个文件。

我尝试了以下内容:

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

我得到了这个例外。怎么做?

1 个答案:

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

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