Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要解析linux命令的输出_Python_Regex - Fatal编程技术网

Python 需要解析linux命令的输出

Python 需要解析linux命令的输出,python,regex,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", :ti

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

我遇到了这个异常。怎么做?

这应该是一个简单的
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)

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

您应该查看
re
文档以了解如何使用它为什么要使用
子流程(grep…
?为什么不只使用
模块re
?我想如果您能更清楚地了解要输入到正则表达式中的输入、要匹配的字符串以及要获取的组,我的人可能会提供帮助。