Python将匹配的行与匹配之前的行连接在一起

问题描述 投票:1回答:1

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

我正在尝试从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
python python-3.x linux pandas
1个回答
1
投票

快速草图:

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.
© www.soinside.com 2019 - 2024. All rights reserved.