2

I want to extract relevant data of an http thread which has been started with a specific UUID from a log file. Example log:

2018-09-26 06:34:24,815 INFO  [com.xxx.xxx.xxx] (http-threads-threads - 73244) UUID: 111-222-333-444-555
2018-09-26 06:34:25,224 WARN  [com.xxx.xxx.xxx] (http-threads-threads - 74391) Some log message
2018-09-26 06:34:26,782 INFO  [com.xxx.xxx.xxx] (http-threads-threads - 74399) Some log message
2018-09-26 06:34:26,945 ERROR [com.xxx.xxx.xxx] (http-threads-threads - 73244) Some exception message of the right thread
    at com.xxx.xxx.xxx(someclass.java:114) [somejar.jar:1.0.0]
    at com.xxx.xxx.xxx(someclass.java:65) [somejar.jar:1.0.0]
    at com.xxx.xxx.xxx(someclass.java:85) [classes:]
2018-09-26 06:34:26,950 ERROR [com.xxx.xxx.xxx] (http-threads-threads - 74256) Unauthorized: com.xxx.xxx.xxx: Unauthorized
    at com.xxx.xxx.xxx(someclass.java:39) [somejar.jar:1.0.0]
    at com.xxx.xxx.xxx(someclass.java:49) [somejar.jar:1.0.0]
    at com.xxx.xxx.xxx(someclass.java:45) [somejar.jar:1.0.0]
2018-09-26 06:34:26,952 INFO  [com.xxx.xxx.xxx] (http-threads-threads - 74395) Some log message
2018-09-26 06:34:27,014 WARN  [com.xxx.xxx.xxx] (http-threads-threads - 73244) Some log message of the right thread
2018-09-26 06:34:27,530 INFO  [com.xxx.xxx.xxx] (http-threads-threads - 74365) Some log message

I can already search for the UUID and extract the thread number using grep and BASH_REMATCH. Knowing the thread number I can search for "http-threads-threads - 73244". Now I want to print all lines with that string and any eventual exception (lines with tabs) after these lines.

I want an output like this:

2018-09-26 06:34:24,815 INFO  [com.xxx.xxx.xxx] (http-threads-threads - 73244) UUID: 111-222-333-444-555
2018-09-26 06:34:26,945 ERROR [com.xxx.xxx.xxx] (http-threads-threads - 73244) Some exception message of the right thread
    at com.xxx.xxx.xxx(someclass.java:114) [somejar.jar:1.0.0]
    at com.xxx.xxx.xxx(someclass.java:65) [somejar.jar:1.0.0]
    at com.xxx.xxx.xxx(someclass.java:85) [classes:]
2018-09-26 06:34:27,014 WARN  [com.xxx.xxx.xxx] (http-threads-threads - 73244) Some log message of the right thread

I can't use grep -A 3 because the amount of tabbed lines after the match is variable.

Using awk '/http\-threads\-threads \- 73244/{print $0; getline}/\tat/{print $0}' log.log also prints other tabbed lines.

Using awk '/http\-threads\-threads \- 73244/{a=1;print}/(2[0-9][0-9][0-9]\-[0-1]\-[0-9])/{a=0}' log.log doesn't print the tabbed lines at all.

A perfect solution would also get rid of the extra "grep" and "BASH_REMATCH" before and use the UUID, but I would be totally fine with a solution which takes the thread number as "input".

Does anyone have a solution for this?

1 Answer 1

4

The following AWK script matches the UUID and outputs the corresponding lines you’re interested in:

#!/usr/bin/awk -f

/UUID: 111-222-333-444-555/ {
    tid = substr($7, 1, length($7) - 1)
}

/^[^\t].*http-threads-threads/ {
    if (substr($7, 1, length($7) -1) == tid) {
        matched = 1
        print
    } else {
        matched = 0
    }
}

/^\t/ && matched

The first block matches the UUID and stores the corresponding thread identifier.

The second block matches lines not starting with tabs, containing “http-threads-threads”. If the seventh field matches the thread identifier, the script notes that we’re in a matching block, and prints the current line; otherwise, the script notes that we’re not in a matching block.

The third block matches lines starting with tabs, when we’re in a matching block, and prints them (printing the current line is the default action).

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .