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

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question



























    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?










    share|improve this question

























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 30 at 13:43









      Jeff Schaller

      33.6k851113




      33.6k851113










      asked Sep 27 at 14:08









      ven_sr

      132




      132




















          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).






          share|improve this answer




















          • Thank you! That is what I was looking for :)
            – ven_sr
            Sep 28 at 7:09










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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).






          share|improve this answer




















          • Thank you! That is what I was looking for :)
            – ven_sr
            Sep 28 at 7:09














          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).






          share|improve this answer




















          • Thank you! That is what I was looking for :)
            – ven_sr
            Sep 28 at 7:09












          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).






          share|improve this answer












          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).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)