gpt4 book ai didi

Python 在匹配之前加入匹配行

转载 作者:行者123 更新时间:2023-12-01 06:21:42 25 4
gpt4 key购买 nike

专家,只是尝试匹配日志文件中的字符串,并在匹配之前添加或加入一行,也就是说..将匹配的行与其前面的一行加入。

我正在尝试下面的代码从 SO 获取提示,但是它可以打印与之前的 Just 行匹配的行。但是我想加入这两个 matched + before line

#!/bin/python3
# Print adding matched line with just one line before
with open('smbd.log.old') as input:
lines = tuple(input)
for i, line in enumerate(lines):
if 'failed' in line or 'Timed' in line:
print(*lines[max(i-1, 0):i], line, sep='') # need help to understand this section

上述代码的输出:

[2020/02/19 04:01:15.729527,  0] ../source3/lib/smbldap.c:998(smbldap_connect_system)
failed to bind to server ldap://myldap1.example.com ldap://myldap2.example.com with dn="cn=sambaAdmin,ou=users,o=services" Error: Can't contact LDAP server

[2020/02/19 04:01:15.729696, 1] ../source3/lib/smbldap.c:1206(get_cached_ldap_connect)
Connection to LDAP server failed for the 1 try!

[2020/02/19 04:01:15.729717, 2] ../source3/passdb/pdb_ldap_util.c:287(smbldap_search_domain_info)
smbldap_search_domain_info: Problem during LDAPsearch: Timed out

示例日志文件数据:

[2020/02/18 08:25:21.228421,  0] ../source3/lib/smbldap.c:998(smbldap_connect_system)
failed to bind to server ldap://myldap1.example.com ldap://myldap2.example.com with dn="cn=sambaAdmin,ou=users,o=services" Error: Can't contact LDAP server
(unknown)
[2020/02/18 08:25:21.229198, 1] ../source3/lib/smbldap.c:1206(get_cached_ldap_connect)
Connection to LDAP server failed for the 1 try!
[2020/02/18 08:25:21.229221, 2] ../source3/passdb/pdb_ldap_util.c:287(smbldap_search_domain_info)
smbldap_search_domain_info: Problem during LDAPsearch: Timed out
[2020/02/18 08:25:21.229229, 2] ../source3/passdb/pdb_ldap_util.c:288(smbldap_search_domain_info)
smbldap_search_domain_info: Query was: ou=TDL,o=HPP, (&(objectClass=sambaDomain)(sambaDomainName=INV1506))
[2020/02/18 08:25:21.229244, 0] ../source3/passdb/pdb_ldap.c:6534(pdb_ldapsam_init_common)
pdb_init_ldapsam: WARNING: Could not get domain info, nor add one to the domain. We cannot work reliably without it.
[2020/02/18 08:25:21.229256, 0] ../source3/passdb/pdb_interface.c:179(make_pdb_method_name)
pdb backend ldapsam:"ldap://myldap1.example.com ldap://myldap2.example.com" did not correctly init (error was NT_STATUS_CANT_ACCESS_DOMAIN_INFO)
[2020/02/19 03:54:46.677689, 1] ../lib/param/loadparm.c:1729(lpcfg_do_global_parameter)
WARNING: The "syslog" option is deprecated

期望:

[2020/02/19 04:01:15.729527,  0] ../source3/lib/smbldap.c:998(smbldap_connect_system) failed to bind to server ldap://myldap1.example.com ldap://myldap2.example.com with dn="cn=sambaAdmin,ou=users,o=services" Error: Can't contact LDAP server

[2020/02/19 04:01:15.729696, 1] ../source3/lib/smbldap.c:1206(get_cached_ldap_connect) Connection to LDAP server failed for the 1 try!

[2020/02/19 04:01:15.729717, 2] ../source3/passdb/pdb_ldap_util.c:287(smbldap_search_domain_info) smbldap_search_domain_info: Problem during LDAPsearch: Timed out

最佳答案

快速草图:

prev_line = "<no previous line>"
with open(...) as input_file:
for line_no, line in enumerate(input_file, start=1):
# Reading one line at a time suffices.
if 'failed' in line or 'Timed' in line:
print(line_no - 1, prev_line)
print(line_no, line)
prev_line = line # Remember for next time.

关于Python 在匹配之前加入匹配行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60325794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com