counting number of three digit numbers in a text file












0















I have a text file with hundreds of three digit numbers.

For example:



 0  2  3
0 2 3
0 2 9
0 3 9
0 9 2
0 9 2
0 9 9
1 2 2
1 2 2
1 2 2
1 2 9
1 2 9
1 3 3
1 9 2
1 9 2
1 9 2
1 9 3
1 9 9
1 9 9
1 9 9
1 9 9
2 0 2
2 0 3
2 0 9
2 1 2
2 1 2
2 1 3
2 1 9
2 1 9
2 2 4
2 2 5
2 2 5
2 2 5
2 2 6
2 2 6
2 2 8


I want to convert this to show how many of each number is in the file to look something like this:



 0  2  3     2
0 9 2 2
1 2 2 3
etc









share|improve this question

























  • the number do not display the right way after I posted the question. Sorry

    – Moe Shii
    Feb 13 at 1:14











  • Are those numbers all composed of single digits? If so, you can create a linear array of 1000 and use the numbers as indices to count their occurrences.

    – zx485
    Feb 13 at 1:37


















0















I have a text file with hundreds of three digit numbers.

For example:



 0  2  3
0 2 3
0 2 9
0 3 9
0 9 2
0 9 2
0 9 9
1 2 2
1 2 2
1 2 2
1 2 9
1 2 9
1 3 3
1 9 2
1 9 2
1 9 2
1 9 3
1 9 9
1 9 9
1 9 9
1 9 9
2 0 2
2 0 3
2 0 9
2 1 2
2 1 2
2 1 3
2 1 9
2 1 9
2 2 4
2 2 5
2 2 5
2 2 5
2 2 6
2 2 6
2 2 8


I want to convert this to show how many of each number is in the file to look something like this:



 0  2  3     2
0 9 2 2
1 2 2 3
etc









share|improve this question

























  • the number do not display the right way after I posted the question. Sorry

    – Moe Shii
    Feb 13 at 1:14











  • Are those numbers all composed of single digits? If so, you can create a linear array of 1000 and use the numbers as indices to count their occurrences.

    – zx485
    Feb 13 at 1:37
















0












0








0








I have a text file with hundreds of three digit numbers.

For example:



 0  2  3
0 2 3
0 2 9
0 3 9
0 9 2
0 9 2
0 9 9
1 2 2
1 2 2
1 2 2
1 2 9
1 2 9
1 3 3
1 9 2
1 9 2
1 9 2
1 9 3
1 9 9
1 9 9
1 9 9
1 9 9
2 0 2
2 0 3
2 0 9
2 1 2
2 1 2
2 1 3
2 1 9
2 1 9
2 2 4
2 2 5
2 2 5
2 2 5
2 2 6
2 2 6
2 2 8


I want to convert this to show how many of each number is in the file to look something like this:



 0  2  3     2
0 9 2 2
1 2 2 3
etc









share|improve this question
















I have a text file with hundreds of three digit numbers.

For example:



 0  2  3
0 2 3
0 2 9
0 3 9
0 9 2
0 9 2
0 9 9
1 2 2
1 2 2
1 2 2
1 2 9
1 2 9
1 3 3
1 9 2
1 9 2
1 9 2
1 9 3
1 9 9
1 9 9
1 9 9
1 9 9
2 0 2
2 0 3
2 0 9
2 1 2
2 1 2
2 1 3
2 1 9
2 1 9
2 2 4
2 2 5
2 2 5
2 2 5
2 2 6
2 2 6
2 2 8


I want to convert this to show how many of each number is in the file to look something like this:



 0  2  3     2
0 9 2 2
1 2 2 3
etc






files batch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 13 at 1:58









zx485

1,47131115




1,47131115










asked Feb 13 at 1:12









Moe ShiiMoe Shii

1




1













  • the number do not display the right way after I posted the question. Sorry

    – Moe Shii
    Feb 13 at 1:14











  • Are those numbers all composed of single digits? If so, you can create a linear array of 1000 and use the numbers as indices to count their occurrences.

    – zx485
    Feb 13 at 1:37





















  • the number do not display the right way after I posted the question. Sorry

    – Moe Shii
    Feb 13 at 1:14











  • Are those numbers all composed of single digits? If so, you can create a linear array of 1000 and use the numbers as indices to count their occurrences.

    – zx485
    Feb 13 at 1:37



















the number do not display the right way after I posted the question. Sorry

– Moe Shii
Feb 13 at 1:14





the number do not display the right way after I posted the question. Sorry

– Moe Shii
Feb 13 at 1:14













Are those numbers all composed of single digits? If so, you can create a linear array of 1000 and use the numbers as indices to count their occurrences.

– zx485
Feb 13 at 1:37







Are those numbers all composed of single digits? If so, you can create a linear array of 1000 and use the numbers as indices to count their occurrences.

– zx485
Feb 13 at 1:37












2 Answers
2






active

oldest

votes


















1














sort -n numbers.txt | uniq -c | sed -E 's/^( *[0-9]+) (.*)$/2 1/' would be the simplest way to achieve your goal. It:





  • sorts your numbers first, just in case


  • counts the length of each unique set


  • seds the result to move the count to the rear of each string






share|improve this answer































    1














    The fact that the lines consist of sequences of digits is largely irrelevant - unless you want to do arithmetic on the numbers themselves you can count / uniquify them just like any other strings e.g. using an associative array or hash:



    awk '{c[$0]++} END {for (i in c) printf "%st%dn", i, c[i]}' numbers.txt


    or



    perl -lnE '$c{$_}++ }{ for $k (keys %c) { say "$kt$c{$k}" }' numbers.txt





    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1117804%2fcounting-number-of-three-digit-numbers-in-a-text-file%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      sort -n numbers.txt | uniq -c | sed -E 's/^( *[0-9]+) (.*)$/2 1/' would be the simplest way to achieve your goal. It:





      • sorts your numbers first, just in case


      • counts the length of each unique set


      • seds the result to move the count to the rear of each string






      share|improve this answer




























        1














        sort -n numbers.txt | uniq -c | sed -E 's/^( *[0-9]+) (.*)$/2 1/' would be the simplest way to achieve your goal. It:





        • sorts your numbers first, just in case


        • counts the length of each unique set


        • seds the result to move the count to the rear of each string






        share|improve this answer


























          1












          1








          1







          sort -n numbers.txt | uniq -c | sed -E 's/^( *[0-9]+) (.*)$/2 1/' would be the simplest way to achieve your goal. It:





          • sorts your numbers first, just in case


          • counts the length of each unique set


          • seds the result to move the count to the rear of each string






          share|improve this answer













          sort -n numbers.txt | uniq -c | sed -E 's/^( *[0-9]+) (.*)$/2 1/' would be the simplest way to achieve your goal. It:





          • sorts your numbers first, just in case


          • counts the length of each unique set


          • seds the result to move the count to the rear of each string







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 13 at 2:25









          AdrianAdrian

          1563




          1563

























              1














              The fact that the lines consist of sequences of digits is largely irrelevant - unless you want to do arithmetic on the numbers themselves you can count / uniquify them just like any other strings e.g. using an associative array or hash:



              awk '{c[$0]++} END {for (i in c) printf "%st%dn", i, c[i]}' numbers.txt


              or



              perl -lnE '$c{$_}++ }{ for $k (keys %c) { say "$kt$c{$k}" }' numbers.txt





              share|improve this answer




























                1














                The fact that the lines consist of sequences of digits is largely irrelevant - unless you want to do arithmetic on the numbers themselves you can count / uniquify them just like any other strings e.g. using an associative array or hash:



                awk '{c[$0]++} END {for (i in c) printf "%st%dn", i, c[i]}' numbers.txt


                or



                perl -lnE '$c{$_}++ }{ for $k (keys %c) { say "$kt$c{$k}" }' numbers.txt





                share|improve this answer


























                  1












                  1








                  1







                  The fact that the lines consist of sequences of digits is largely irrelevant - unless you want to do arithmetic on the numbers themselves you can count / uniquify them just like any other strings e.g. using an associative array or hash:



                  awk '{c[$0]++} END {for (i in c) printf "%st%dn", i, c[i]}' numbers.txt


                  or



                  perl -lnE '$c{$_}++ }{ for $k (keys %c) { say "$kt$c{$k}" }' numbers.txt





                  share|improve this answer













                  The fact that the lines consist of sequences of digits is largely irrelevant - unless you want to do arithmetic on the numbers themselves you can count / uniquify them just like any other strings e.g. using an associative array or hash:



                  awk '{c[$0]++} END {for (i in c) printf "%st%dn", i, c[i]}' numbers.txt


                  or



                  perl -lnE '$c{$_}++ }{ for $k (keys %c) { say "$kt$c{$k}" }' numbers.txt






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 13 at 2:49









                  steeldriversteeldriver

                  68.9k11113184




                  68.9k11113184






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • 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%2faskubuntu.com%2fquestions%2f1117804%2fcounting-number-of-three-digit-numbers-in-a-text-file%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

                      flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                      Mangá

                      Eduardo VII do Reino Unido