Count number of string occurrences [duplicate]

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











up vote
3
down vote

favorite













This question already has an answer here:



  • Get text-file word occurrence count of all words & print output sorted

    6 answers



I've extracted strings I'm interested in from another file and now have a list like this:



StringA
StringB
StringA
StringA
StringB
StringC
StringB


How can I extract the number of occurrences each string has using common command-line tools?



I would like to end up with a list like this:



StringA 3
StringB 3
StringC 1






share|improve this question













marked as duplicate by Sergiy Kolodyazhnyy, slm♦ Jul 5 at 21:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    That is NOT json.
    – ctrl-alt-delor
    Jul 5 at 13:45










  • @ctrl-alt-delor let me quote from above "and I've extracted strings I'm interested in" - I didn't say that the shown snippet is JSON!
    – cerr
    Jul 5 at 13:52







  • 1




    So what am I supposed to do with that information. How is it relevant that it was JSON. May be it could be used to create a different answer, that processes the JSON, so yes put it at the end. But at the beginning, I have to parse that, and realise that it is not relevant. It just adds more effort for the reader.
    – ctrl-alt-delor
    Jul 5 at 13:55










  • I was half-tempted to write a pure-Bash solution, but I think that's not what you want, so let's edit away the reference to that particular shell.
    – ilkkachu
    Jul 5 at 13:58














up vote
3
down vote

favorite













This question already has an answer here:



  • Get text-file word occurrence count of all words & print output sorted

    6 answers



I've extracted strings I'm interested in from another file and now have a list like this:



StringA
StringB
StringA
StringA
StringB
StringC
StringB


How can I extract the number of occurrences each string has using common command-line tools?



I would like to end up with a list like this:



StringA 3
StringB 3
StringC 1






share|improve this question













marked as duplicate by Sergiy Kolodyazhnyy, slm♦ Jul 5 at 21:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    That is NOT json.
    – ctrl-alt-delor
    Jul 5 at 13:45










  • @ctrl-alt-delor let me quote from above "and I've extracted strings I'm interested in" - I didn't say that the shown snippet is JSON!
    – cerr
    Jul 5 at 13:52







  • 1




    So what am I supposed to do with that information. How is it relevant that it was JSON. May be it could be used to create a different answer, that processes the JSON, so yes put it at the end. But at the beginning, I have to parse that, and realise that it is not relevant. It just adds more effort for the reader.
    – ctrl-alt-delor
    Jul 5 at 13:55










  • I was half-tempted to write a pure-Bash solution, but I think that's not what you want, so let's edit away the reference to that particular shell.
    – ilkkachu
    Jul 5 at 13:58












up vote
3
down vote

favorite









up vote
3
down vote

favorite












This question already has an answer here:



  • Get text-file word occurrence count of all words & print output sorted

    6 answers



I've extracted strings I'm interested in from another file and now have a list like this:



StringA
StringB
StringA
StringA
StringB
StringC
StringB


How can I extract the number of occurrences each string has using common command-line tools?



I would like to end up with a list like this:



StringA 3
StringB 3
StringC 1






share|improve this question














This question already has an answer here:



  • Get text-file word occurrence count of all words & print output sorted

    6 answers



I've extracted strings I'm interested in from another file and now have a list like this:



StringA
StringB
StringA
StringA
StringB
StringC
StringB


How can I extract the number of occurrences each string has using common command-line tools?



I would like to end up with a list like this:



StringA 3
StringB 3
StringC 1




This question already has an answer here:



  • Get text-file word occurrence count of all words & print output sorted

    6 answers









share|improve this question












share|improve this question




share|improve this question








edited Jul 5 at 20:52









Peter Mortensen

76948




76948









asked Jul 5 at 13:05









cerr

61772236




61772236




marked as duplicate by Sergiy Kolodyazhnyy, slm♦ Jul 5 at 21:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Sergiy Kolodyazhnyy, slm♦ Jul 5 at 21:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 2




    That is NOT json.
    – ctrl-alt-delor
    Jul 5 at 13:45










  • @ctrl-alt-delor let me quote from above "and I've extracted strings I'm interested in" - I didn't say that the shown snippet is JSON!
    – cerr
    Jul 5 at 13:52







  • 1




    So what am I supposed to do with that information. How is it relevant that it was JSON. May be it could be used to create a different answer, that processes the JSON, so yes put it at the end. But at the beginning, I have to parse that, and realise that it is not relevant. It just adds more effort for the reader.
    – ctrl-alt-delor
    Jul 5 at 13:55










  • I was half-tempted to write a pure-Bash solution, but I think that's not what you want, so let's edit away the reference to that particular shell.
    – ilkkachu
    Jul 5 at 13:58












  • 2




    That is NOT json.
    – ctrl-alt-delor
    Jul 5 at 13:45










  • @ctrl-alt-delor let me quote from above "and I've extracted strings I'm interested in" - I didn't say that the shown snippet is JSON!
    – cerr
    Jul 5 at 13:52







  • 1




    So what am I supposed to do with that information. How is it relevant that it was JSON. May be it could be used to create a different answer, that processes the JSON, so yes put it at the end. But at the beginning, I have to parse that, and realise that it is not relevant. It just adds more effort for the reader.
    – ctrl-alt-delor
    Jul 5 at 13:55










  • I was half-tempted to write a pure-Bash solution, but I think that's not what you want, so let's edit away the reference to that particular shell.
    – ilkkachu
    Jul 5 at 13:58







2




2




That is NOT json.
– ctrl-alt-delor
Jul 5 at 13:45




That is NOT json.
– ctrl-alt-delor
Jul 5 at 13:45












@ctrl-alt-delor let me quote from above "and I've extracted strings I'm interested in" - I didn't say that the shown snippet is JSON!
– cerr
Jul 5 at 13:52





@ctrl-alt-delor let me quote from above "and I've extracted strings I'm interested in" - I didn't say that the shown snippet is JSON!
– cerr
Jul 5 at 13:52





1




1




So what am I supposed to do with that information. How is it relevant that it was JSON. May be it could be used to create a different answer, that processes the JSON, so yes put it at the end. But at the beginning, I have to parse that, and realise that it is not relevant. It just adds more effort for the reader.
– ctrl-alt-delor
Jul 5 at 13:55




So what am I supposed to do with that information. How is it relevant that it was JSON. May be it could be used to create a different answer, that processes the JSON, so yes put it at the end. But at the beginning, I have to parse that, and realise that it is not relevant. It just adds more effort for the reader.
– ctrl-alt-delor
Jul 5 at 13:55












I was half-tempted to write a pure-Bash solution, but I think that's not what you want, so let's edit away the reference to that particular shell.
– ilkkachu
Jul 5 at 13:58




I was half-tempted to write a pure-Bash solution, but I think that's not what you want, so let's edit away the reference to that particular shell.
– ilkkachu
Jul 5 at 13:58










2 Answers
2






active

oldest

votes

















up vote
8
down vote



accepted










Use:



sort file | uniq -c


Looks simple?






share|improve this answer





















  • That's it! my extraction stuff |sort|uniq -c does the trick! Great, Thanks!
    – cerr
    Jul 5 at 13:13

















up vote
6
down vote













With GNU datamash:



datamash -s -g1 count 1 <file


The output:



StringA 3
StringB 3
StringC 1





share|improve this answer




























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    8
    down vote



    accepted










    Use:



    sort file | uniq -c


    Looks simple?






    share|improve this answer





















    • That's it! my extraction stuff |sort|uniq -c does the trick! Great, Thanks!
      – cerr
      Jul 5 at 13:13














    up vote
    8
    down vote



    accepted










    Use:



    sort file | uniq -c


    Looks simple?






    share|improve this answer





















    • That's it! my extraction stuff |sort|uniq -c does the trick! Great, Thanks!
      – cerr
      Jul 5 at 13:13












    up vote
    8
    down vote



    accepted







    up vote
    8
    down vote



    accepted






    Use:



    sort file | uniq -c


    Looks simple?






    share|improve this answer













    Use:



    sort file | uniq -c


    Looks simple?







    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Jul 5 at 13:09









    schily

    8,53921435




    8,53921435











    • That's it! my extraction stuff |sort|uniq -c does the trick! Great, Thanks!
      – cerr
      Jul 5 at 13:13
















    • That's it! my extraction stuff |sort|uniq -c does the trick! Great, Thanks!
      – cerr
      Jul 5 at 13:13















    That's it! my extraction stuff |sort|uniq -c does the trick! Great, Thanks!
    – cerr
    Jul 5 at 13:13




    That's it! my extraction stuff |sort|uniq -c does the trick! Great, Thanks!
    – cerr
    Jul 5 at 13:13












    up vote
    6
    down vote













    With GNU datamash:



    datamash -s -g1 count 1 <file


    The output:



    StringA 3
    StringB 3
    StringC 1





    share|improve this answer

























      up vote
      6
      down vote













      With GNU datamash:



      datamash -s -g1 count 1 <file


      The output:



      StringA 3
      StringB 3
      StringC 1





      share|improve this answer























        up vote
        6
        down vote










        up vote
        6
        down vote









        With GNU datamash:



        datamash -s -g1 count 1 <file


        The output:



        StringA 3
        StringB 3
        StringC 1





        share|improve this answer













        With GNU datamash:



        datamash -s -g1 count 1 <file


        The output:



        StringA 3
        StringB 3
        StringC 1






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jul 5 at 13:11









        RomanPerekhrest

        22.4k12144




        22.4k12144












            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)