awk/sed/grep: Printing all lines matching a string and all lines with tabs after these lines

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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?
text-processing awk sed grep logs
add a comment |Â
up vote
2
down vote
favorite
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?
text-processing awk sed grep logs
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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?
text-processing awk sed grep logs
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?
text-processing awk sed grep logs
text-processing awk sed grep logs
edited Sep 30 at 13:43
Jeff Schaller
33.6k851113
33.6k851113
asked Sep 27 at 14:08
ven_sr
132
132
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
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).
Thank you! That is what I was looking for :)
â ven_sr
Sep 28 at 7:09
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
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).
Thank you! That is what I was looking for :)
â ven_sr
Sep 28 at 7:09
add a comment |Â
up vote
4
down vote
accepted
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).
Thank you! That is what I was looking for :)
â ven_sr
Sep 28 at 7:09
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
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).
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).
answered Sep 27 at 14:24
Stephen Kitt
149k23328396
149k23328396
Thank you! That is what I was looking for :)
â ven_sr
Sep 28 at 7:09
add a comment |Â
Thank you! That is what I was looking for :)
â ven_sr
Sep 28 at 7:09
Thank you! That is what I was looking for :)
â ven_sr
Sep 28 at 7:09
Thank you! That is what I was looking for :)
â ven_sr
Sep 28 at 7:09
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471835%2fawk-sed-grep-printing-all-lines-matching-a-string-and-all-lines-with-tabs-after%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password