【发布时间】:2020-02-20 12:26:11
【问题描述】:
期待在Linux主机上的一个文件中打印Matching line,并将匹配行之前的一行打印到一行中。
以下只是日志文件中的内容:
[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
我尝试过的:
我已尝试使用 grep 和 sed 进行跟踪,这在某种程度上可行..
$ egrep -B 1 "failed|Timed" /var/log/samba/smbd.log.old |tr -d "\n" | sed "s/--/\n/g"
[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
这看起来不是一个更清洁的解决方案,我期待一些专家的单行,awk、sed、grep 甚至 python 都可以接受一个行。
【问题讨论】: