Select lines with two occurences of same pattern












1















I have several lines of output, some lines contain information on jobs waiting
example
I want to select lines that have more than one occurrence of the pattern
jobs waiting: 0



example:



abcdef rglk,jobs waiting: 2,blah,blah,jobs waiting:0,jobs running: 1,blah,blah
lbf(kjn fk)kkj,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
gdjhgvdjh,jobs waiting: 0,blah,blah,jobs running: 1,blah,blah,jobs waiting: 0
g gg,jobs waiting: 2,blah,jobs waiting: 0,jobs running: 1,blah,blah
kjn dikfc,jobs waiting: 0,blah,jobs waiting: 0,jobs running: 1,bl ah,blah
d1d,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
kjfdk nrf(lkj rgf),jobs waiting: 2,blah,blah,jobs waiting: 0,bl ah,blah


would work if the command only returns line 3 and line 5
I would also like to be able to select lines that are opposite to this
Unfortunately I have no idea how to do this, with sed? awk? grep?










share|improve this question









New contributor




David Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    1















    I have several lines of output, some lines contain information on jobs waiting
    example
    I want to select lines that have more than one occurrence of the pattern
    jobs waiting: 0



    example:



    abcdef rglk,jobs waiting: 2,blah,blah,jobs waiting:0,jobs running: 1,blah,blah
    lbf(kjn fk)kkj,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
    gdjhgvdjh,jobs waiting: 0,blah,blah,jobs running: 1,blah,blah,jobs waiting: 0
    g gg,jobs waiting: 2,blah,jobs waiting: 0,jobs running: 1,blah,blah
    kjn dikfc,jobs waiting: 0,blah,jobs waiting: 0,jobs running: 1,bl ah,blah
    d1d,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
    kjfdk nrf(lkj rgf),jobs waiting: 2,blah,blah,jobs waiting: 0,bl ah,blah


    would work if the command only returns line 3 and line 5
    I would also like to be able to select lines that are opposite to this
    Unfortunately I have no idea how to do this, with sed? awk? grep?










    share|improve this question









    New contributor




    David Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      1












      1








      1


      0






      I have several lines of output, some lines contain information on jobs waiting
      example
      I want to select lines that have more than one occurrence of the pattern
      jobs waiting: 0



      example:



      abcdef rglk,jobs waiting: 2,blah,blah,jobs waiting:0,jobs running: 1,blah,blah
      lbf(kjn fk)kkj,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
      gdjhgvdjh,jobs waiting: 0,blah,blah,jobs running: 1,blah,blah,jobs waiting: 0
      g gg,jobs waiting: 2,blah,jobs waiting: 0,jobs running: 1,blah,blah
      kjn dikfc,jobs waiting: 0,blah,jobs waiting: 0,jobs running: 1,bl ah,blah
      d1d,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
      kjfdk nrf(lkj rgf),jobs waiting: 2,blah,blah,jobs waiting: 0,bl ah,blah


      would work if the command only returns line 3 and line 5
      I would also like to be able to select lines that are opposite to this
      Unfortunately I have no idea how to do this, with sed? awk? grep?










      share|improve this question









      New contributor




      David Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I have several lines of output, some lines contain information on jobs waiting
      example
      I want to select lines that have more than one occurrence of the pattern
      jobs waiting: 0



      example:



      abcdef rglk,jobs waiting: 2,blah,blah,jobs waiting:0,jobs running: 1,blah,blah
      lbf(kjn fk)kkj,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
      gdjhgvdjh,jobs waiting: 0,blah,blah,jobs running: 1,blah,blah,jobs waiting: 0
      g gg,jobs waiting: 2,blah,jobs waiting: 0,jobs running: 1,blah,blah
      kjn dikfc,jobs waiting: 0,blah,jobs waiting: 0,jobs running: 1,bl ah,blah
      d1d,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
      kjfdk nrf(lkj rgf),jobs waiting: 2,blah,blah,jobs waiting: 0,bl ah,blah


      would work if the command only returns line 3 and line 5
      I would also like to be able to select lines that are opposite to this
      Unfortunately I have no idea how to do this, with sed? awk? grep?







      awk sed grep






      share|improve this question









      New contributor




      David Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      David Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 22 mins ago









      Sparhawk

      9,94464296




      9,94464296






      New contributor




      David Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 34 mins ago









      David JenkinsDavid Jenkins

      61




      61




      New contributor




      David Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      David Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      David Jenkins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          1














          KISS grep approach:



          grep 'jobs waiting: *0.*jobs waiting: *0' file


          Invert by adding the -v command line switch.



          Alternate with sed - attempt to replace the second instance, and print the line if it succeeds:



          sed -n 's/jobs waiting: *0/&/2p' file


          Inverse as



          sed -n 's/jobs waiting: *0/&/2; t; p' file


          Awk approach - use the return value of a gsub:



          awk 'gsub(/jobs waiting: *0/,"&",$0) == 2' file


          Invert as



          awk 'gsub(/jobs waiting: *0/,"&",$0) != 2' file


          (In all cases, *0 allows for zero or more space characters before the 0, consistent with your example.)






          share|improve this answer


























          • May I ask what is "KISS"?

            – Tim
            11 mins ago













          • @Tim KISS principle

            – steeldriver
            9 mins ago











          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          David Jenkins is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500974%2fselect-lines-with-two-occurences-of-same-pattern%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          KISS grep approach:



          grep 'jobs waiting: *0.*jobs waiting: *0' file


          Invert by adding the -v command line switch.



          Alternate with sed - attempt to replace the second instance, and print the line if it succeeds:



          sed -n 's/jobs waiting: *0/&/2p' file


          Inverse as



          sed -n 's/jobs waiting: *0/&/2; t; p' file


          Awk approach - use the return value of a gsub:



          awk 'gsub(/jobs waiting: *0/,"&",$0) == 2' file


          Invert as



          awk 'gsub(/jobs waiting: *0/,"&",$0) != 2' file


          (In all cases, *0 allows for zero or more space characters before the 0, consistent with your example.)






          share|improve this answer


























          • May I ask what is "KISS"?

            – Tim
            11 mins ago













          • @Tim KISS principle

            – steeldriver
            9 mins ago
















          1














          KISS grep approach:



          grep 'jobs waiting: *0.*jobs waiting: *0' file


          Invert by adding the -v command line switch.



          Alternate with sed - attempt to replace the second instance, and print the line if it succeeds:



          sed -n 's/jobs waiting: *0/&/2p' file


          Inverse as



          sed -n 's/jobs waiting: *0/&/2; t; p' file


          Awk approach - use the return value of a gsub:



          awk 'gsub(/jobs waiting: *0/,"&",$0) == 2' file


          Invert as



          awk 'gsub(/jobs waiting: *0/,"&",$0) != 2' file


          (In all cases, *0 allows for zero or more space characters before the 0, consistent with your example.)






          share|improve this answer


























          • May I ask what is "KISS"?

            – Tim
            11 mins ago













          • @Tim KISS principle

            – steeldriver
            9 mins ago














          1












          1








          1







          KISS grep approach:



          grep 'jobs waiting: *0.*jobs waiting: *0' file


          Invert by adding the -v command line switch.



          Alternate with sed - attempt to replace the second instance, and print the line if it succeeds:



          sed -n 's/jobs waiting: *0/&/2p' file


          Inverse as



          sed -n 's/jobs waiting: *0/&/2; t; p' file


          Awk approach - use the return value of a gsub:



          awk 'gsub(/jobs waiting: *0/,"&",$0) == 2' file


          Invert as



          awk 'gsub(/jobs waiting: *0/,"&",$0) != 2' file


          (In all cases, *0 allows for zero or more space characters before the 0, consistent with your example.)






          share|improve this answer















          KISS grep approach:



          grep 'jobs waiting: *0.*jobs waiting: *0' file


          Invert by adding the -v command line switch.



          Alternate with sed - attempt to replace the second instance, and print the line if it succeeds:



          sed -n 's/jobs waiting: *0/&/2p' file


          Inverse as



          sed -n 's/jobs waiting: *0/&/2; t; p' file


          Awk approach - use the return value of a gsub:



          awk 'gsub(/jobs waiting: *0/,"&",$0) == 2' file


          Invert as



          awk 'gsub(/jobs waiting: *0/,"&",$0) != 2' file


          (In all cases, *0 allows for zero or more space characters before the 0, consistent with your example.)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 5 mins ago

























          answered 20 mins ago









          steeldriversteeldriver

          36.1k35286




          36.1k35286













          • May I ask what is "KISS"?

            – Tim
            11 mins ago













          • @Tim KISS principle

            – steeldriver
            9 mins ago



















          • May I ask what is "KISS"?

            – Tim
            11 mins ago













          • @Tim KISS principle

            – steeldriver
            9 mins ago

















          May I ask what is "KISS"?

          – Tim
          11 mins ago







          May I ask what is "KISS"?

          – Tim
          11 mins ago















          @Tim KISS principle

          – steeldriver
          9 mins ago





          @Tim KISS principle

          – steeldriver
          9 mins ago










          David Jenkins is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          David Jenkins is a new contributor. Be nice, and check out our Code of Conduct.













          David Jenkins is a new contributor. Be nice, and check out our Code of Conduct.












          David Jenkins is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500974%2fselect-lines-with-two-occurences-of-same-pattern%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          宮崎県

          濃尾地震

          シテ島