Filter list based on number of digits in strings

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











up vote
2
down vote

favorite












I have long list of entries are recorded in a file, something like this short list:



FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
6846DFEC-C0A2-11E8-B7A8-3ECB9C0CC049
6846DFEC-C0A2-11E8-B7A8-3ECB9C0CC049
. . . .

. . . .

. . . .


I want to filter this list based on the number of digits within each entry and if the number of digits more than a specific threshold then keep the string otherwise remove it. in the previous example, how can I keep all the entries that have 18 digits in its names?



Expected out put:



FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049


Very big probelm here please help!!!










share|improve this question

















  • 1




    All of those have 32 digits; they just happen to be hexidecimal digits.
    – DopeGhoti
    3 hours ago














up vote
2
down vote

favorite












I have long list of entries are recorded in a file, something like this short list:



FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
6846DFEC-C0A2-11E8-B7A8-3ECB9C0CC049
6846DFEC-C0A2-11E8-B7A8-3ECB9C0CC049
. . . .

. . . .

. . . .


I want to filter this list based on the number of digits within each entry and if the number of digits more than a specific threshold then keep the string otherwise remove it. in the previous example, how can I keep all the entries that have 18 digits in its names?



Expected out put:



FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049


Very big probelm here please help!!!










share|improve this question

















  • 1




    All of those have 32 digits; they just happen to be hexidecimal digits.
    – DopeGhoti
    3 hours ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have long list of entries are recorded in a file, something like this short list:



FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
6846DFEC-C0A2-11E8-B7A8-3ECB9C0CC049
6846DFEC-C0A2-11E8-B7A8-3ECB9C0CC049
. . . .

. . . .

. . . .


I want to filter this list based on the number of digits within each entry and if the number of digits more than a specific threshold then keep the string otherwise remove it. in the previous example, how can I keep all the entries that have 18 digits in its names?



Expected out put:



FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049


Very big probelm here please help!!!










share|improve this question













I have long list of entries are recorded in a file, something like this short list:



FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
6846DFEC-C0A2-11E8-B7A8-3ECB9C0CC049
6846DFEC-C0A2-11E8-B7A8-3ECB9C0CC049
. . . .

. . . .

. . . .


I want to filter this list based on the number of digits within each entry and if the number of digits more than a specific threshold then keep the string otherwise remove it. in the previous example, how can I keep all the entries that have 18 digits in its names?



Expected out put:



FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049


Very big probelm here please help!!!







bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 3 hours ago









Zahi

956




956







  • 1




    All of those have 32 digits; they just happen to be hexidecimal digits.
    – DopeGhoti
    3 hours ago












  • 1




    All of those have 32 digits; they just happen to be hexidecimal digits.
    – DopeGhoti
    3 hours ago







1




1




All of those have 32 digits; they just happen to be hexidecimal digits.
– DopeGhoti
3 hours ago




All of those have 32 digits; they just happen to be hexidecimal digits.
– DopeGhoti
3 hours ago










3 Answers
3






active

oldest

votes

















up vote
4
down vote













With awk:



awk -F '[[:digit:]]' 'NF > 18'


We use digits as the field separator, so the number of fields will be one plus the number of digits (x1y is split into x and y), so above we're looking for lines that have at least 18 digits.



(with mawk, replace [:digit:] with 0-9. mawk doesn't support POSIX character classes, but it's [0-9] contrary to other awk implementations matches on 0123456789 only regardless of the locale. Portably, you can use [0123456789], or you can use [0-9] is you know the text doesn't contain non-ASCII data).



For lines that have exactly 18 digits, that would be:



awk -F '[[:digit:]]' 'NF == 19'


With sed, for at least 18 digits:



sed -e 's/[[:digit:]]/&/18;t' -e d


With grep:



grep -E '(.*[[:digit:]])18'





share|improve this answer





























    up vote
    1
    down vote













    Let's say that the data are saved in a file called file.txt, then you can do something like:



    #!/bin/bash
    cat file.txt | while IFS= read line; do

    n=$(echo $line | awk 'print gsub("[0-9]", "")')
    if [[ $n -gt 17 ]]; then

    echo $line
    fi
    done

    FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
    682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
    682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049


    Or



    awk 'gsub("[0-9]", "&") >= 18'





    share|improve this answer






















    • or, more directly, awk 'if (gsub("[0-9]", "") > 17) print;' < file.txt
      – Jeff Schaller
      3 hours ago







    • 2




      @JeffSchaller, you'd want awk 'gsub("[0-9]", "&") >= 18' instead.
      – Stéphane Chazelas
      3 hours ago










    • @Stéphane Chazelas. Correct thanks!
      – Goro
      3 hours ago










    • @Jeff Schaller I really don't know, this is what come to mind, very old style ;-)
      – Goro
      3 hours ago

















    up vote
    0
    down vote













    How about using the return value of perl's tr (similar to using the returnvalue of awk's gsub)



    $ perl -ne 'print if tr0-90-9 >= 18' file
    FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
    682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
    682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049




    share




















      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%2f472106%2ffilter-list-based-on-number-of-digits-in-strings%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
      4
      down vote













      With awk:



      awk -F '[[:digit:]]' 'NF > 18'


      We use digits as the field separator, so the number of fields will be one plus the number of digits (x1y is split into x and y), so above we're looking for lines that have at least 18 digits.



      (with mawk, replace [:digit:] with 0-9. mawk doesn't support POSIX character classes, but it's [0-9] contrary to other awk implementations matches on 0123456789 only regardless of the locale. Portably, you can use [0123456789], or you can use [0-9] is you know the text doesn't contain non-ASCII data).



      For lines that have exactly 18 digits, that would be:



      awk -F '[[:digit:]]' 'NF == 19'


      With sed, for at least 18 digits:



      sed -e 's/[[:digit:]]/&/18;t' -e d


      With grep:



      grep -E '(.*[[:digit:]])18'





      share|improve this answer


























        up vote
        4
        down vote













        With awk:



        awk -F '[[:digit:]]' 'NF > 18'


        We use digits as the field separator, so the number of fields will be one plus the number of digits (x1y is split into x and y), so above we're looking for lines that have at least 18 digits.



        (with mawk, replace [:digit:] with 0-9. mawk doesn't support POSIX character classes, but it's [0-9] contrary to other awk implementations matches on 0123456789 only regardless of the locale. Portably, you can use [0123456789], or you can use [0-9] is you know the text doesn't contain non-ASCII data).



        For lines that have exactly 18 digits, that would be:



        awk -F '[[:digit:]]' 'NF == 19'


        With sed, for at least 18 digits:



        sed -e 's/[[:digit:]]/&/18;t' -e d


        With grep:



        grep -E '(.*[[:digit:]])18'





        share|improve this answer
























          up vote
          4
          down vote










          up vote
          4
          down vote









          With awk:



          awk -F '[[:digit:]]' 'NF > 18'


          We use digits as the field separator, so the number of fields will be one plus the number of digits (x1y is split into x and y), so above we're looking for lines that have at least 18 digits.



          (with mawk, replace [:digit:] with 0-9. mawk doesn't support POSIX character classes, but it's [0-9] contrary to other awk implementations matches on 0123456789 only regardless of the locale. Portably, you can use [0123456789], or you can use [0-9] is you know the text doesn't contain non-ASCII data).



          For lines that have exactly 18 digits, that would be:



          awk -F '[[:digit:]]' 'NF == 19'


          With sed, for at least 18 digits:



          sed -e 's/[[:digit:]]/&/18;t' -e d


          With grep:



          grep -E '(.*[[:digit:]])18'





          share|improve this answer














          With awk:



          awk -F '[[:digit:]]' 'NF > 18'


          We use digits as the field separator, so the number of fields will be one plus the number of digits (x1y is split into x and y), so above we're looking for lines that have at least 18 digits.



          (with mawk, replace [:digit:] with 0-9. mawk doesn't support POSIX character classes, but it's [0-9] contrary to other awk implementations matches on 0123456789 only regardless of the locale. Portably, you can use [0123456789], or you can use [0-9] is you know the text doesn't contain non-ASCII data).



          For lines that have exactly 18 digits, that would be:



          awk -F '[[:digit:]]' 'NF == 19'


          With sed, for at least 18 digits:



          sed -e 's/[[:digit:]]/&/18;t' -e d


          With grep:



          grep -E '(.*[[:digit:]])18'






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago

























          answered 3 hours ago









          Stéphane Chazelas

          286k53528866




          286k53528866






















              up vote
              1
              down vote













              Let's say that the data are saved in a file called file.txt, then you can do something like:



              #!/bin/bash
              cat file.txt | while IFS= read line; do

              n=$(echo $line | awk 'print gsub("[0-9]", "")')
              if [[ $n -gt 17 ]]; then

              echo $line
              fi
              done

              FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
              682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
              682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049


              Or



              awk 'gsub("[0-9]", "&") >= 18'





              share|improve this answer






















              • or, more directly, awk 'if (gsub("[0-9]", "") > 17) print;' < file.txt
                – Jeff Schaller
                3 hours ago







              • 2




                @JeffSchaller, you'd want awk 'gsub("[0-9]", "&") >= 18' instead.
                – Stéphane Chazelas
                3 hours ago










              • @Stéphane Chazelas. Correct thanks!
                – Goro
                3 hours ago










              • @Jeff Schaller I really don't know, this is what come to mind, very old style ;-)
                – Goro
                3 hours ago














              up vote
              1
              down vote













              Let's say that the data are saved in a file called file.txt, then you can do something like:



              #!/bin/bash
              cat file.txt | while IFS= read line; do

              n=$(echo $line | awk 'print gsub("[0-9]", "")')
              if [[ $n -gt 17 ]]; then

              echo $line
              fi
              done

              FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
              682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
              682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049


              Or



              awk 'gsub("[0-9]", "&") >= 18'





              share|improve this answer






















              • or, more directly, awk 'if (gsub("[0-9]", "") > 17) print;' < file.txt
                – Jeff Schaller
                3 hours ago







              • 2




                @JeffSchaller, you'd want awk 'gsub("[0-9]", "&") >= 18' instead.
                – Stéphane Chazelas
                3 hours ago










              • @Stéphane Chazelas. Correct thanks!
                – Goro
                3 hours ago










              • @Jeff Schaller I really don't know, this is what come to mind, very old style ;-)
                – Goro
                3 hours ago












              up vote
              1
              down vote










              up vote
              1
              down vote









              Let's say that the data are saved in a file called file.txt, then you can do something like:



              #!/bin/bash
              cat file.txt | while IFS= read line; do

              n=$(echo $line | awk 'print gsub("[0-9]", "")')
              if [[ $n -gt 17 ]]; then

              echo $line
              fi
              done

              FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
              682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
              682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049


              Or



              awk 'gsub("[0-9]", "&") >= 18'





              share|improve this answer














              Let's say that the data are saved in a file called file.txt, then you can do something like:



              #!/bin/bash
              cat file.txt | while IFS= read line; do

              n=$(echo $line | awk 'print gsub("[0-9]", "")')
              if [[ $n -gt 17 ]]; then

              echo $line
              fi
              done

              FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
              682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
              682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049


              Or



              awk 'gsub("[0-9]", "&") >= 18'






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 3 hours ago

























              answered 3 hours ago









              Goro

              5,05552459




              5,05552459











              • or, more directly, awk 'if (gsub("[0-9]", "") > 17) print;' < file.txt
                – Jeff Schaller
                3 hours ago







              • 2




                @JeffSchaller, you'd want awk 'gsub("[0-9]", "&") >= 18' instead.
                – Stéphane Chazelas
                3 hours ago










              • @Stéphane Chazelas. Correct thanks!
                – Goro
                3 hours ago










              • @Jeff Schaller I really don't know, this is what come to mind, very old style ;-)
                – Goro
                3 hours ago
















              • or, more directly, awk 'if (gsub("[0-9]", "") > 17) print;' < file.txt
                – Jeff Schaller
                3 hours ago







              • 2




                @JeffSchaller, you'd want awk 'gsub("[0-9]", "&") >= 18' instead.
                – Stéphane Chazelas
                3 hours ago










              • @Stéphane Chazelas. Correct thanks!
                – Goro
                3 hours ago










              • @Jeff Schaller I really don't know, this is what come to mind, very old style ;-)
                – Goro
                3 hours ago















              or, more directly, awk 'if (gsub("[0-9]", "") > 17) print;' < file.txt
              – Jeff Schaller
              3 hours ago





              or, more directly, awk 'if (gsub("[0-9]", "") > 17) print;' < file.txt
              – Jeff Schaller
              3 hours ago





              2




              2




              @JeffSchaller, you'd want awk 'gsub("[0-9]", "&") >= 18' instead.
              – Stéphane Chazelas
              3 hours ago




              @JeffSchaller, you'd want awk 'gsub("[0-9]", "&") >= 18' instead.
              – Stéphane Chazelas
              3 hours ago












              @Stéphane Chazelas. Correct thanks!
              – Goro
              3 hours ago




              @Stéphane Chazelas. Correct thanks!
              – Goro
              3 hours ago












              @Jeff Schaller I really don't know, this is what come to mind, very old style ;-)
              – Goro
              3 hours ago




              @Jeff Schaller I really don't know, this is what come to mind, very old style ;-)
              – Goro
              3 hours ago










              up vote
              0
              down vote













              How about using the return value of perl's tr (similar to using the returnvalue of awk's gsub)



              $ perl -ne 'print if tr0-90-9 >= 18' file
              FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
              682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
              682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049




              share
























                up vote
                0
                down vote













                How about using the return value of perl's tr (similar to using the returnvalue of awk's gsub)



                $ perl -ne 'print if tr0-90-9 >= 18' file
                FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
                682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
                682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049




                share






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  How about using the return value of perl's tr (similar to using the returnvalue of awk's gsub)



                  $ perl -ne 'print if tr0-90-9 >= 18' file
                  FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
                  682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
                  682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049




                  share












                  How about using the return value of perl's tr (similar to using the returnvalue of awk's gsub)



                  $ perl -ne 'print if tr0-90-9 >= 18' file
                  FFF1B976-9DDE-11E7-9C3D-6241D7D553BE
                  682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049
                  682D9DB6-C0A2-11E8-B7A8-3ECB9C0CC049





                  share











                  share


                  share










                  answered 2 mins ago









                  steeldriver

                  32.2k34979




                  32.2k34979



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f472106%2ffilter-list-based-on-number-of-digits-in-strings%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      Installing NextGIS Connect into QGIS 3?

                      One-line joke