How to print the count of pattern at each line?

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











up vote
6
down vote

favorite












I need to print the count of a matching string at the end of each line.



An example for matching foo:



foo,bar,foo,foo
bar,foo,bar,bar
foo,foo,bar,bar


Result :



foo,bar,foo,foo,3
bar,foo,bar,bar,1
foo,foo,bar,bar,2


I have checked this link(How to count the number of a specific character in each line?) but no luck.







share|improve this question


























    up vote
    6
    down vote

    favorite












    I need to print the count of a matching string at the end of each line.



    An example for matching foo:



    foo,bar,foo,foo
    bar,foo,bar,bar
    foo,foo,bar,bar


    Result :



    foo,bar,foo,foo,3
    bar,foo,bar,bar,1
    foo,foo,bar,bar,2


    I have checked this link(How to count the number of a specific character in each line?) but no luck.







    share|improve this question
























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I need to print the count of a matching string at the end of each line.



      An example for matching foo:



      foo,bar,foo,foo
      bar,foo,bar,bar
      foo,foo,bar,bar


      Result :



      foo,bar,foo,foo,3
      bar,foo,bar,bar,1
      foo,foo,bar,bar,2


      I have checked this link(How to count the number of a specific character in each line?) but no luck.







      share|improve this question














      I need to print the count of a matching string at the end of each line.



      An example for matching foo:



      foo,bar,foo,foo
      bar,foo,bar,bar
      foo,foo,bar,bar


      Result :



      foo,bar,foo,foo,3
      bar,foo,bar,bar,1
      foo,foo,bar,bar,2


      I have checked this link(How to count the number of a specific character in each line?) but no luck.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 25 at 21:32









      Jeff Schaller

      32k849109




      32k849109










      asked Aug 25 at 17:44









      dgear01

      334




      334




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          11
          down vote



          accepted










          We can use awk with gsub to get the count of occurrence.



           awk 'print $0","gsub(/foo/,"")' file


          Output:



          foo,bar,foo,foo,3
          bar,foo,bar,bar,1
          foo,foo,bar,bar,2





          share|improve this answer
















          • 1




            Worked like charm!!! thanks.
            – dgear01
            Aug 25 at 17:53

















          up vote
          1
          down vote













          Perhaps with a mixture of bash and grep



          $ while read -r line; do 
          echo -n "$line -> "
          grep -o foo <<<"$line" | wc -l
          done < /path/to/my-input-file

          foo,bar,foo,foo -> 3
          bar,foo,bar,bar -> 1
          foo,foo,bar,bar -> 2





          share|improve this answer



























            up vote
            0
            down vote













            In case anyone also wanted something in Python. Specify the filename and the magic word to count over.



            #!/usr/bin/python3
            # magic_word_count.py
            # Takes a filename and magic word and prints the number of times the word
            # appears on each line of the file.
            #
            # ./magic_word_count.py myfile.txt foo
            #
            import sys
            filename = sys.argv[1]
            magic_word = sys.argv[2]

            with open(filename, 'r') as f:
            for line in f.readlines():
            words = line.strip().split(',')
            print(len([word for word in words if word == magic_word]))


            Usage:



            $ cat myfile.txt 
            foo,bar,foo,foo
            bar,foo,bar,bar
            foo,foo,bar,bar

            $ ./magic_word_count.py myfile.txt foo
            3
            1
            2





            share|improve this answer




















              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%2f464818%2fhow-to-print-the-count-of-pattern-at-each-line%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              11
              down vote



              accepted










              We can use awk with gsub to get the count of occurrence.



               awk 'print $0","gsub(/foo/,"")' file


              Output:



              foo,bar,foo,foo,3
              bar,foo,bar,bar,1
              foo,foo,bar,bar,2





              share|improve this answer
















              • 1




                Worked like charm!!! thanks.
                – dgear01
                Aug 25 at 17:53














              up vote
              11
              down vote



              accepted










              We can use awk with gsub to get the count of occurrence.



               awk 'print $0","gsub(/foo/,"")' file


              Output:



              foo,bar,foo,foo,3
              bar,foo,bar,bar,1
              foo,foo,bar,bar,2





              share|improve this answer
















              • 1




                Worked like charm!!! thanks.
                – dgear01
                Aug 25 at 17:53












              up vote
              11
              down vote



              accepted







              up vote
              11
              down vote



              accepted






              We can use awk with gsub to get the count of occurrence.



               awk 'print $0","gsub(/foo/,"")' file


              Output:



              foo,bar,foo,foo,3
              bar,foo,bar,bar,1
              foo,foo,bar,bar,2





              share|improve this answer












              We can use awk with gsub to get the count of occurrence.



               awk 'print $0","gsub(/foo/,"")' file


              Output:



              foo,bar,foo,foo,3
              bar,foo,bar,bar,1
              foo,foo,bar,bar,2






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 25 at 17:47









              SivaPrasath

              1




              1







              • 1




                Worked like charm!!! thanks.
                – dgear01
                Aug 25 at 17:53












              • 1




                Worked like charm!!! thanks.
                – dgear01
                Aug 25 at 17:53







              1




              1




              Worked like charm!!! thanks.
              – dgear01
              Aug 25 at 17:53




              Worked like charm!!! thanks.
              – dgear01
              Aug 25 at 17:53












              up vote
              1
              down vote













              Perhaps with a mixture of bash and grep



              $ while read -r line; do 
              echo -n "$line -> "
              grep -o foo <<<"$line" | wc -l
              done < /path/to/my-input-file

              foo,bar,foo,foo -> 3
              bar,foo,bar,bar -> 1
              foo,foo,bar,bar -> 2





              share|improve this answer
























                up vote
                1
                down vote













                Perhaps with a mixture of bash and grep



                $ while read -r line; do 
                echo -n "$line -> "
                grep -o foo <<<"$line" | wc -l
                done < /path/to/my-input-file

                foo,bar,foo,foo -> 3
                bar,foo,bar,bar -> 1
                foo,foo,bar,bar -> 2





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Perhaps with a mixture of bash and grep



                  $ while read -r line; do 
                  echo -n "$line -> "
                  grep -o foo <<<"$line" | wc -l
                  done < /path/to/my-input-file

                  foo,bar,foo,foo -> 3
                  bar,foo,bar,bar -> 1
                  foo,foo,bar,bar -> 2





                  share|improve this answer












                  Perhaps with a mixture of bash and grep



                  $ while read -r line; do 
                  echo -n "$line -> "
                  grep -o foo <<<"$line" | wc -l
                  done < /path/to/my-input-file

                  foo,bar,foo,foo -> 3
                  bar,foo,bar,bar -> 1
                  foo,foo,bar,bar -> 2






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 25 at 20:56









                  shalomb

                  1585




                  1585




















                      up vote
                      0
                      down vote













                      In case anyone also wanted something in Python. Specify the filename and the magic word to count over.



                      #!/usr/bin/python3
                      # magic_word_count.py
                      # Takes a filename and magic word and prints the number of times the word
                      # appears on each line of the file.
                      #
                      # ./magic_word_count.py myfile.txt foo
                      #
                      import sys
                      filename = sys.argv[1]
                      magic_word = sys.argv[2]

                      with open(filename, 'r') as f:
                      for line in f.readlines():
                      words = line.strip().split(',')
                      print(len([word for word in words if word == magic_word]))


                      Usage:



                      $ cat myfile.txt 
                      foo,bar,foo,foo
                      bar,foo,bar,bar
                      foo,foo,bar,bar

                      $ ./magic_word_count.py myfile.txt foo
                      3
                      1
                      2





                      share|improve this answer
























                        up vote
                        0
                        down vote













                        In case anyone also wanted something in Python. Specify the filename and the magic word to count over.



                        #!/usr/bin/python3
                        # magic_word_count.py
                        # Takes a filename and magic word and prints the number of times the word
                        # appears on each line of the file.
                        #
                        # ./magic_word_count.py myfile.txt foo
                        #
                        import sys
                        filename = sys.argv[1]
                        magic_word = sys.argv[2]

                        with open(filename, 'r') as f:
                        for line in f.readlines():
                        words = line.strip().split(',')
                        print(len([word for word in words if word == magic_word]))


                        Usage:



                        $ cat myfile.txt 
                        foo,bar,foo,foo
                        bar,foo,bar,bar
                        foo,foo,bar,bar

                        $ ./magic_word_count.py myfile.txt foo
                        3
                        1
                        2





                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          In case anyone also wanted something in Python. Specify the filename and the magic word to count over.



                          #!/usr/bin/python3
                          # magic_word_count.py
                          # Takes a filename and magic word and prints the number of times the word
                          # appears on each line of the file.
                          #
                          # ./magic_word_count.py myfile.txt foo
                          #
                          import sys
                          filename = sys.argv[1]
                          magic_word = sys.argv[2]

                          with open(filename, 'r') as f:
                          for line in f.readlines():
                          words = line.strip().split(',')
                          print(len([word for word in words if word == magic_word]))


                          Usage:



                          $ cat myfile.txt 
                          foo,bar,foo,foo
                          bar,foo,bar,bar
                          foo,foo,bar,bar

                          $ ./magic_word_count.py myfile.txt foo
                          3
                          1
                          2





                          share|improve this answer












                          In case anyone also wanted something in Python. Specify the filename and the magic word to count over.



                          #!/usr/bin/python3
                          # magic_word_count.py
                          # Takes a filename and magic word and prints the number of times the word
                          # appears on each line of the file.
                          #
                          # ./magic_word_count.py myfile.txt foo
                          #
                          import sys
                          filename = sys.argv[1]
                          magic_word = sys.argv[2]

                          with open(filename, 'r') as f:
                          for line in f.readlines():
                          words = line.strip().split(',')
                          print(len([word for word in words if word == magic_word]))


                          Usage:



                          $ cat myfile.txt 
                          foo,bar,foo,foo
                          bar,foo,bar,bar
                          foo,foo,bar,bar

                          $ ./magic_word_count.py myfile.txt foo
                          3
                          1
                          2






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 25 at 23:57









                          user1717828

                          1,59611125




                          1,59611125



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f464818%2fhow-to-print-the-count-of-pattern-at-each-line%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              Long meetings (6-7 hours a day): Being “babysat” by supervisor

                              Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                              Confectionery