1

I have a log file myfile.log which is having below content

Thu Jun 04 09:02:05 2020
Closing scheduler window
Closing Resource Manager plan via scheduler window
Clearing Resource Manager plan at database via parameter
Thu Jun 04 09:22:50 2020
mystring3: APP failed to died. Likely the connection
has been lost. PKCS11 function C_EncryptInit returned
PKCS11 error code: 6
APP connection lost, closing wallet
Thu Jun 04 09:32:20 2020
Incremental point up to RBA [0x5a5f.43feb8.0], current log tail at RBA [0x5a5f.44d433.0]
Thu Jun 04 09:39:54 2020
mystring3: APP failed died. Likely the connection
has been lost. PKCS11 function C_EncryptInit returned
PKCS11 error code: 6
APP connection lost, closing wallet
Thu Jun 04 09:51:12 2020
mystring1: APP failed check failed to cache
object handle. Error code: 1014
APP connection lost, closing wallet
Thu Jun 04 09:51:18 2020
Errors in file /folder1/folder2/folder3/file.log:
Err-9087: cpu time or run time policy violation
Thu Jun 04 09:52:07 2020
Dumping diagnostic data in directory=[cdmp_20200604095207], requested by (instance=8, osid=313861), summary=[incident=969964].
Thu Jun 04 10:02:24 2020
Incremental point up to RBA [0x5a5f.48846c.0], current log tail at RBA [0x5a5f.4a1de0.0]
TABLE SYS.USER: ADDED INTERVAL PARTITION SYS_P3375 (3814) VALUES LESS THAN (TO_DATE(' 2020-06-11 01:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'CAL=GREGORIAN'))

I grep "mystring" and fetch the few lines in this format:

grep -B1 -A2 mystring myfile.log | grep "Jun 04 09" -A3

Thu Jun 04 09:22:50 2020
mystring3: APP failed to died. Likely the connection
has been lost. PKCS11 function C_EncryptInit returned
PKCS11 error code: 6
--
Thu Jun 04 09:39:54 2020
mystring3: APP failed died. Likely the connection
has been lost. PKCS11 function C_EncryptInit returned
PKCS11 error code: 6
--
Thu Jun 04 09:51:12 2020
mystring1: APP failed check failed to cache
object handle. Error code: 1014
APP connection lost, closing wallet

But I want to fetch the lines in below format where I need the line which is having "Incremental point" with the above time stamp. I can increase "after row parameter" (-A2) to (-A5) something but how can I fetch the sting with the name in same grep command so that it will not miss the "Incremental point" string's line if it happens after many lines from my searching string or given time frame (Jun 04 09)

Thu Jun 04 09:22:50 2020
mystring3: APP failed to died. Likely the connection
has been lost. PKCS11 function C_EncryptInit returned
PKCS11 error code: 6
Thu Jun 04 09:32:20 2020
Incremental point up to RBA [0x5a5f.43feb8.0], current log tail at RBA [0x5a5f.44d433.0]
--
Thu Jun 04 09:39:54 2020
mystring3: APP failed died. Likely the connection
has been lost. PKCS11 function C_EncryptInit returned
PKCS11 error code: 6
--
Thu Jun 04 09:51:12 2020
mystring1: APP failed check failed to cache
object handle. Error code: 1014
APP connection lost, closing wallet
Thu Jun 04 10:02:24 2020
Incremental point up to RBA [0x5a5f.48846c.0], current log tail at RBA [0x5a5f.4a1de0.0]
2
  • 1
    Please, read MCVE – Gilles Quenot Jun 22 '20 at 18:13
  • 1
    @GillesQuenot I apologies for a longer example. I just wanted to give an exact scenario so that it will be clear to understand. My log file content 45 thousands lines and I tried to squeezed it as much as possible. – Jeetu Jun 22 '20 at 18:19
2

Try this, This will work for you i think :

$ awk -v d="Jun 04 09" 'BEGIN { n=1;i=1;start=0;split(d,mon);extrahour=mon[1]" "mon[2]" "mon[3]+1;}

/mystring/ { if (i==1 && start == 1 ){ n=split(a,b,"\n");for (j=1;j<n;j++) print b[j];a="";  }

if (start == 1) { print "-----\n"l"\n"$0;n=2;i=1;next} }

/^Incremental/ { if (start==1) { print a"\n"$0;a="";n=1;i=0; }}

{ if( $0 ~ d )start=1; if ( $0 ~ mon[1] &&  ( $0 !~ d && $0 !~ extrahour ) ) {start=0;} 

  if ( start==1 ) {  l=$0; if (n==2) a=a"\n"$0  }

 } ' file | sed /^$/d

-----
Thu Jun 04 09:22:50 2020
mystring3: APP failed to died. Likely the connection
has been lost. PKCS11 function C_EncryptInit returned
PKCS11 error code: 6
APP connection lost, closing wallet
Thu Jun 04 09:32:20 2020
Incremental point up to RBA [0x5a5f.43feb8.0], current log tail at RBA [0x5a5f.44d433.0]
-----
Thu Jun 04 09:39:54 2020
mystring3: APP failed died. Likely the connection
has been lost. PKCS11 function C_EncryptInit returned
PKCS11 error code: 6
APP connection lost, closing wallet
-----
Thu Jun 04 09:51:12 2020
mystring1: APP failed check failed to cache
object handle. Error code: 1014
APP connection lost, closing wallet
Thu Jun 04 09:51:18 2020
Errors in file /folder1/folder2/folder3/file.log:
Err-9087: cpu time or run time policy violation
Thu Jun 04 09:52:07 2020
Dumping diagnostic data in directory=[cdmp_20200604095207], requested by (instance=8, osid=313861), summary=[incident=969964].
Thu Jun 04 10:02:24 2020
Incremental point up to RBA [0x5a5f.48846c.0], current log tail at RBA [0x5a5f.4a1de0.0]
1
0
$ awk -v searchstring=mystring 'print_line; '\
'/^Incremental point/ { print_line=0; print ""; print "------------"; print ""; next; }; '\
'$0 ~ "^" searchstring { print; print_line=1; }' input

mystring3: APP failed to died. Likely the connection
has been lost. PKCS11 function C_EncryptInit returned
PKCS11 error code: 6
APP connection lost, closing wallet
Thu Jun 04 09:32:20 2020
Incremental point up to RBA [0x5a5f.43feb8.0], current log tail at RBA [0x5a5f.44d433.0]

------------

mystring3: APP failed died. Likely the connection
has been lost. PKCS11 function C_EncryptInit returned
PKCS11 error code: 6
APP connection lost, closing wallet
Thu Jun 04 09:51:12 2020
mystring1: APP failed check failed to cache
mystring1: APP failed check failed to cache
object handle. Error code: 1014
APP connection lost, closing wallet
Thu Jun 04 09:51:18 2020
Errors in file /folder1/folder2/folder3/file.log:
Err-9087: cpu time or run time policy violation
Thu Jun 04 09:52:07 2020
Dumping diagnostic data in directory=[cdmp_20200604095207], requested by (instance=8, osid=313861), summary=[incident=969964].
Thu Jun 04 10:02:24 2020
Incremental point up to RBA [0x5a5f.48846c.0], current log tail at RBA [0x5a5f.4a1de0.0]

------------
2
  • I need the "Incremental Point" string line with two conditions, first the time which I mention in my grep (Jun 04 09) and also at last after matching the "mystring" for the given timeframe. – Jeetu Jun 23 '20 at 7:21
  • Can you please help me to get the solution on date based also along with "mystring" string – Jeetu Jun 23 '20 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.