Print Matching line and one line before from the matched line

kulfi :

looking forward to print Matching line in a file on Linux host and one line before from the matched line included into one line.

Below is just the content from the log file:

[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

What i have tried:

I have tried following with grep and sed which somehow works..

$ 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

This does not looks to be a cleaner solution, i'm looking forward some expert one lines, one liner is acceptable with awk, sed, grep or even python.

Błotosmętek :

It can be done with awk alone:

awk ' /Timed|failed/ { print previous, $0; }; {previous = $0;}' /var/log/samba/smbd.log.old

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=6569&siteId=1