1

i have a scenario where i need to prepare/write exceptions to a file from the log file by grepping Instance name and writing all its related exceptions to Instance.txt

example test-dom-01.txt,test-dom-01.txt,test-dom-04.txt ...so on with its associated errors

out.log

Performing Health Check for Instance : test-dom-01
test-dom-01 Instance is----->  Running
****Existing Exceptions***** on instance: test-dom-01
catalina.0.log:INFO: Error parsing HTTP request header
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in the HTTP protocol
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in 
catalina.0.log:SEVERE: Exception when trying to read white list from  probably because file list is empty

--> Health Check for Instance :test-dom-01 is completed

Performing Health Check for Instance : tests-rs-dom2-1
tests-rs-dom2-1 Instance is----->  Running
****Existing Exceptions***** on instance: tests-rs-dom2-1 
catalina.0.log:INFO: Error parsing HTTP request header
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
catalina.out:   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)2019-09-19 15:23:03.266 [xxxx BACK END TCPLink Reader (Server-555)]
catalina.out:   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)2019-09-19 15:28:53 INFO
catalina.0.log:Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
catalina.0.log:org.apache.catalina.LifecycleException: Failed to initialize component [Connector[AJP/1.3-7002]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardService[Catalina]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize connector [Connector[AJP/1.3-7002]]
catalina.out:java.lang.Error: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]


--> Health Check for Instance :tests-rs-dom2-1 is completed

Performing Health Check for Instance : test-dom-04
test-dom-04 Instance is----->  Running
****Existing Exceptions***** on instance: test-dom-04
catalina.0.log:Caused by: java.net.BindException: Address already in use
catalina.0.log:Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
catalina.0.log:org.apache.catalina.LifecycleException: Failed to initialize component [Connector[AJP/1.3-7002]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardService[Catalina]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize connector [Connector[AJP/1.3-7002]]
catalina.out:java.lang.Error: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]


--> Health Check for Instance :test-dom-04 is completed

The Ask is i have to move the existing exceptions to each file ignoring the rest of the lines just exceptions have to move to separate file

Expected output for test-dom-01 file is test-dom-01.txt and content is

catalina.0.log:INFO: Error parsing HTTP request header
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in the HTTP protocol
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in 
catalina.0.log:SEVERE: Exception when trying to read white list from  probably because file list is empty

Expected out put for tests-rs-dom2-1 is tests-rs-dom2-1.txt

catalina.0.log:INFO: Error parsing HTTP request header
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
catalina.out:   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)2019-09-19 15:23:03.266 [xxxx BACK END TCPLink Reader (Server-555)]
catalina.out:   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)2019-09-19 15:28:53 INFO
catalina.0.log:Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
catalina.0.log:org.apache.catalina.LifecycleException: Failed to initialize component [Connector[AJP/1.3-7002]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardService[Catalina]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize connector [Connector[AJP/1.3-7002]]
catalina.out:java.lang.Error: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]

Tried:

awk '/on instance: test-dom-01/,/ /' out.log > test-dom-01.txt

but no luck

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Rczone
  • 493
  • 1
  • 5
  • 18

1 Answers1

2

You may use this awk:

awk '/ on instance: /{fn=$NF ".txt"; next} !NF{close(fn); fn=""} fn{print > fn}' out.log
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • NF: Event not found. @anubhava – Rczone Sep 27 '19 at 19:44
  • You can replace `!NF` with `NF==0` also. – anubhava Sep 27 '19 at 19:47
  • copied as is No luck and also replaced with NF==0 but got no output @anubhava – Rczone Sep 27 '19 at 19:57
  • All i see $ in between line breaks when i use cat -A --i dont think it uses DOS breaks as my script is running on Unix and even the generated input file is from unix @anubhava – Rczone Sep 27 '19 at 20:07
  • You can see a working demo here: https://ideone.com/90agq5 (since we cannot create file on ideone so I made minor modifications in code) – anubhava Sep 27 '19 at 20:10
  • 1
    Thankyou for your time @anubhava appreciate it..will figure it out from here – Rczone Sep 27 '19 at 20:14
  • Just one tip. You can run `awk '/ on instance: /{fn="*** " $NF ".txt ***"; print fn; next} !NF{fn=""} fn' out.log` to check if you're getting same output as my ideone demo or not. – anubhava Sep 27 '19 at 20:26
  • yes its working now...but whatever i have given is snippet of the log file but its not working on actual log which has lot of lines before and after the snippet i have given here @anubhava – Rczone Sep 27 '19 at 20:29
  • 1
    @Rczone wrt your first comment `NF: Event not found` - your shell is apparently interpreting the `!` in `!NF` as a history lookup, see https://unix.stackexchange.com/q/33339/133219 for example. Are you **sure** you're using single quotes around the script as shown in the answer? If your real data doesn't look like the sample you provided then fix the sample you provided so it does. All we have to go on to try to help you is the info you provide in your question. – Ed Morton Sep 28 '19 at 00:49
  • 2
    @EdMorton Thankyou..you have been very helpful even in my previous questions..im still learning things. – Rczone Oct 01 '19 at 17:34