awk: print text between two patterns + x lines followed by first match

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












Following Input File:



#Report Nr. 2343215
#Errors 3243
#Date: (Timestampt)
#Informaiton
#
# Headers
# Specs
DLSLWD 0 0 0 0 Jun 22 01:51:16PM 2018
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
#Reports
#
Error-Number 123
Error Number 12345
#


What i need is an awk that pipes the "List of Objects" into a new file:



#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#


And the " Reports"into a differnt file:



#Reports
#
Error-Number 123
Error Number 12345
#


It's a match for #List of Objects + 3 lines until "first" #.



Same for the Reports: Match #Reports + 1 line until "first" #.



At first i tried something like:



awk '/#List of Objects/,/#Reports/'


For the list of objects followed by:



awk '/#Reports/,0'


To get the data from #Reports until EOF.



But because #Reports and #List of Objects are both OPTIONAL and not in every input file I can't use #Reports as the "END-Pattern". so, I have to match on the # but ignore the first X occurrences of # after the matching pattern.







share|improve this question

























    up vote
    3
    down vote

    favorite
    1












    Following Input File:



    #Report Nr. 2343215
    #Errors 3243
    #Date: (Timestampt)
    #Informaiton
    #
    # Headers
    # Specs
    DLSLWD 0 0 0 0 Jun 22 01:51:16PM 2018
    #List of Objects
    #
    # Headers
    # Paths
    Files not found /var/xxxxx
    Files not found /etc/xxxxx
    Files not found /mnt/xxxxx
    Files not found /safd/xxxxx
    #
    #Reports
    #
    Error-Number 123
    Error Number 12345
    #


    What i need is an awk that pipes the "List of Objects" into a new file:



    #List of Objects
    #
    # Headers
    # Paths
    Files not found /var/xxxxx
    Files not found /etc/xxxxx
    Files not found /mnt/xxxxx
    Files not found /safd/xxxxx
    #


    And the " Reports"into a differnt file:



    #Reports
    #
    Error-Number 123
    Error Number 12345
    #


    It's a match for #List of Objects + 3 lines until "first" #.



    Same for the Reports: Match #Reports + 1 line until "first" #.



    At first i tried something like:



    awk '/#List of Objects/,/#Reports/'


    For the list of objects followed by:



    awk '/#Reports/,0'


    To get the data from #Reports until EOF.



    But because #Reports and #List of Objects are both OPTIONAL and not in every input file I can't use #Reports as the "END-Pattern". so, I have to match on the # but ignore the first X occurrences of # after the matching pattern.







    share|improve this question























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      Following Input File:



      #Report Nr. 2343215
      #Errors 3243
      #Date: (Timestampt)
      #Informaiton
      #
      # Headers
      # Specs
      DLSLWD 0 0 0 0 Jun 22 01:51:16PM 2018
      #List of Objects
      #
      # Headers
      # Paths
      Files not found /var/xxxxx
      Files not found /etc/xxxxx
      Files not found /mnt/xxxxx
      Files not found /safd/xxxxx
      #
      #Reports
      #
      Error-Number 123
      Error Number 12345
      #


      What i need is an awk that pipes the "List of Objects" into a new file:



      #List of Objects
      #
      # Headers
      # Paths
      Files not found /var/xxxxx
      Files not found /etc/xxxxx
      Files not found /mnt/xxxxx
      Files not found /safd/xxxxx
      #


      And the " Reports"into a differnt file:



      #Reports
      #
      Error-Number 123
      Error Number 12345
      #


      It's a match for #List of Objects + 3 lines until "first" #.



      Same for the Reports: Match #Reports + 1 line until "first" #.



      At first i tried something like:



      awk '/#List of Objects/,/#Reports/'


      For the list of objects followed by:



      awk '/#Reports/,0'


      To get the data from #Reports until EOF.



      But because #Reports and #List of Objects are both OPTIONAL and not in every input file I can't use #Reports as the "END-Pattern". so, I have to match on the # but ignore the first X occurrences of # after the matching pattern.







      share|improve this question













      Following Input File:



      #Report Nr. 2343215
      #Errors 3243
      #Date: (Timestampt)
      #Informaiton
      #
      # Headers
      # Specs
      DLSLWD 0 0 0 0 Jun 22 01:51:16PM 2018
      #List of Objects
      #
      # Headers
      # Paths
      Files not found /var/xxxxx
      Files not found /etc/xxxxx
      Files not found /mnt/xxxxx
      Files not found /safd/xxxxx
      #
      #Reports
      #
      Error-Number 123
      Error Number 12345
      #


      What i need is an awk that pipes the "List of Objects" into a new file:



      #List of Objects
      #
      # Headers
      # Paths
      Files not found /var/xxxxx
      Files not found /etc/xxxxx
      Files not found /mnt/xxxxx
      Files not found /safd/xxxxx
      #


      And the " Reports"into a differnt file:



      #Reports
      #
      Error-Number 123
      Error Number 12345
      #


      It's a match for #List of Objects + 3 lines until "first" #.



      Same for the Reports: Match #Reports + 1 line until "first" #.



      At first i tried something like:



      awk '/#List of Objects/,/#Reports/'


      For the list of objects followed by:



      awk '/#Reports/,0'


      To get the data from #Reports until EOF.



      But because #Reports and #List of Objects are both OPTIONAL and not in every input file I can't use #Reports as the "END-Pattern". so, I have to match on the # but ignore the first X occurrences of # after the matching pattern.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 27 at 15:13









      SivaPrasath

      3,88611737




      3,88611737









      asked Jun 27 at 14:15









      T-One

      336




      336




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Awk script:



          extract_pat_space.awk contents:



          $0 ~ "^#" pat f = 1; hash = 0 
          f print
          NF == 1 && $1 == "#"
          if (++hash == 2) f = hash = 0




          Usage (for both patterns):



          $ awk -f extract_pat_space.awk -v pat="List" file > list_of_objects.txt
          $ awk -f extract_pat_space.awk -v pat="Reports" file > reports.txt



          Results:



          $ cat list_of_objects.txt 
          #List of Objects
          #
          # Headers
          # Paths
          Files not found /var/xxxxx
          Files not found /etc/xxxxx
          Files not found /mnt/xxxxx
          Files not found /safd/xxxxx
          #



          $ cat reports.txt 
          #Reports
          #
          Error-Number 123
          Error Number 12345
          #





          share|improve this answer





















          • i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
            – T-One
            Jun 27 at 15:10










          • @T-One, extend your question according to the mentioned moment
            – RomanPerekhrest
            Jun 27 at 15:13

















          up vote
          1
          down vote













          awk '
          /#List of Objects/ f = "objects.txt"
          /#Reports/ f = "reports.txt"
          f print > f
          ' file


          When you see one of the key headers, set the output filename.

          If the variable has been set, print to that file.






          share|improve this answer





















          • This looks extremly simple, how does awk know where to stop?
            – T-One
            Jun 28 at 7:27











          • Stop? At the end of file. Are there other criteria I missed?
            – glenn jackman
            Jun 28 at 10:16










          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%2f452234%2fawk-print-text-between-two-patterns-x-lines-followed-by-first-match%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Awk script:



          extract_pat_space.awk contents:



          $0 ~ "^#" pat f = 1; hash = 0 
          f print
          NF == 1 && $1 == "#"
          if (++hash == 2) f = hash = 0




          Usage (for both patterns):



          $ awk -f extract_pat_space.awk -v pat="List" file > list_of_objects.txt
          $ awk -f extract_pat_space.awk -v pat="Reports" file > reports.txt



          Results:



          $ cat list_of_objects.txt 
          #List of Objects
          #
          # Headers
          # Paths
          Files not found /var/xxxxx
          Files not found /etc/xxxxx
          Files not found /mnt/xxxxx
          Files not found /safd/xxxxx
          #



          $ cat reports.txt 
          #Reports
          #
          Error-Number 123
          Error Number 12345
          #





          share|improve this answer





















          • i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
            – T-One
            Jun 27 at 15:10










          • @T-One, extend your question according to the mentioned moment
            – RomanPerekhrest
            Jun 27 at 15:13














          up vote
          1
          down vote



          accepted










          Awk script:



          extract_pat_space.awk contents:



          $0 ~ "^#" pat f = 1; hash = 0 
          f print
          NF == 1 && $1 == "#"
          if (++hash == 2) f = hash = 0




          Usage (for both patterns):



          $ awk -f extract_pat_space.awk -v pat="List" file > list_of_objects.txt
          $ awk -f extract_pat_space.awk -v pat="Reports" file > reports.txt



          Results:



          $ cat list_of_objects.txt 
          #List of Objects
          #
          # Headers
          # Paths
          Files not found /var/xxxxx
          Files not found /etc/xxxxx
          Files not found /mnt/xxxxx
          Files not found /safd/xxxxx
          #



          $ cat reports.txt 
          #Reports
          #
          Error-Number 123
          Error Number 12345
          #





          share|improve this answer





















          • i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
            – T-One
            Jun 27 at 15:10










          • @T-One, extend your question according to the mentioned moment
            – RomanPerekhrest
            Jun 27 at 15:13












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Awk script:



          extract_pat_space.awk contents:



          $0 ~ "^#" pat f = 1; hash = 0 
          f print
          NF == 1 && $1 == "#"
          if (++hash == 2) f = hash = 0




          Usage (for both patterns):



          $ awk -f extract_pat_space.awk -v pat="List" file > list_of_objects.txt
          $ awk -f extract_pat_space.awk -v pat="Reports" file > reports.txt



          Results:



          $ cat list_of_objects.txt 
          #List of Objects
          #
          # Headers
          # Paths
          Files not found /var/xxxxx
          Files not found /etc/xxxxx
          Files not found /mnt/xxxxx
          Files not found /safd/xxxxx
          #



          $ cat reports.txt 
          #Reports
          #
          Error-Number 123
          Error Number 12345
          #





          share|improve this answer













          Awk script:



          extract_pat_space.awk contents:



          $0 ~ "^#" pat f = 1; hash = 0 
          f print
          NF == 1 && $1 == "#"
          if (++hash == 2) f = hash = 0




          Usage (for both patterns):



          $ awk -f extract_pat_space.awk -v pat="List" file > list_of_objects.txt
          $ awk -f extract_pat_space.awk -v pat="Reports" file > reports.txt



          Results:



          $ cat list_of_objects.txt 
          #List of Objects
          #
          # Headers
          # Paths
          Files not found /var/xxxxx
          Files not found /etc/xxxxx
          Files not found /mnt/xxxxx
          Files not found /safd/xxxxx
          #



          $ cat reports.txt 
          #Reports
          #
          Error-Number 123
          Error Number 12345
          #






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 27 at 14:37









          RomanPerekhrest

          22.4k12144




          22.4k12144











          • i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
            – T-One
            Jun 27 at 15:10










          • @T-One, extend your question according to the mentioned moment
            – RomanPerekhrest
            Jun 27 at 15:13
















          • i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
            – T-One
            Jun 27 at 15:10










          • @T-One, extend your question according to the mentioned moment
            – RomanPerekhrest
            Jun 27 at 15:13















          i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
          – T-One
          Jun 27 at 15:10




          i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
          – T-One
          Jun 27 at 15:10












          @T-One, extend your question according to the mentioned moment
          – RomanPerekhrest
          Jun 27 at 15:13




          @T-One, extend your question according to the mentioned moment
          – RomanPerekhrest
          Jun 27 at 15:13












          up vote
          1
          down vote













          awk '
          /#List of Objects/ f = "objects.txt"
          /#Reports/ f = "reports.txt"
          f print > f
          ' file


          When you see one of the key headers, set the output filename.

          If the variable has been set, print to that file.






          share|improve this answer





















          • This looks extremly simple, how does awk know where to stop?
            – T-One
            Jun 28 at 7:27











          • Stop? At the end of file. Are there other criteria I missed?
            – glenn jackman
            Jun 28 at 10:16














          up vote
          1
          down vote













          awk '
          /#List of Objects/ f = "objects.txt"
          /#Reports/ f = "reports.txt"
          f print > f
          ' file


          When you see one of the key headers, set the output filename.

          If the variable has been set, print to that file.






          share|improve this answer





















          • This looks extremly simple, how does awk know where to stop?
            – T-One
            Jun 28 at 7:27











          • Stop? At the end of file. Are there other criteria I missed?
            – glenn jackman
            Jun 28 at 10:16












          up vote
          1
          down vote










          up vote
          1
          down vote









          awk '
          /#List of Objects/ f = "objects.txt"
          /#Reports/ f = "reports.txt"
          f print > f
          ' file


          When you see one of the key headers, set the output filename.

          If the variable has been set, print to that file.






          share|improve this answer













          awk '
          /#List of Objects/ f = "objects.txt"
          /#Reports/ f = "reports.txt"
          f print > f
          ' file


          When you see one of the key headers, set the output filename.

          If the variable has been set, print to that file.







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 27 at 16:47









          glenn jackman

          45.6k264100




          45.6k264100











          • This looks extremly simple, how does awk know where to stop?
            – T-One
            Jun 28 at 7:27











          • Stop? At the end of file. Are there other criteria I missed?
            – glenn jackman
            Jun 28 at 10:16
















          • This looks extremly simple, how does awk know where to stop?
            – T-One
            Jun 28 at 7:27











          • Stop? At the end of file. Are there other criteria I missed?
            – glenn jackman
            Jun 28 at 10:16















          This looks extremly simple, how does awk know where to stop?
          – T-One
          Jun 28 at 7:27





          This looks extremly simple, how does awk know where to stop?
          – T-One
          Jun 28 at 7:27













          Stop? At the end of file. Are there other criteria I missed?
          – glenn jackman
          Jun 28 at 10:16




          Stop? At the end of file. Are there other criteria I missed?
          – glenn jackman
          Jun 28 at 10:16












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452234%2fawk-print-text-between-two-patterns-x-lines-followed-by-first-match%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?