How to grep any digit from sentence

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











up vote
-4
down vote

favorite












Could You help me with greping?
I have a:



variable="RMN quota: 0 bytes"


and



variable="RMN quota: 1.56 bytes"


The target id to get output: 0 or 1.56.



What will be grep for it?







share|improve this question






















  • it must be read from a variable.
    – Andrzej TI
    Nov 27 '17 at 19:28














up vote
-4
down vote

favorite












Could You help me with greping?
I have a:



variable="RMN quota: 0 bytes"


and



variable="RMN quota: 1.56 bytes"


The target id to get output: 0 or 1.56.



What will be grep for it?







share|improve this question






















  • it must be read from a variable.
    – Andrzej TI
    Nov 27 '17 at 19:28












up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











Could You help me with greping?
I have a:



variable="RMN quota: 0 bytes"


and



variable="RMN quota: 1.56 bytes"


The target id to get output: 0 or 1.56.



What will be grep for it?







share|improve this question














Could You help me with greping?
I have a:



variable="RMN quota: 0 bytes"


and



variable="RMN quota: 1.56 bytes"


The target id to get output: 0 or 1.56.



What will be grep for it?









share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '17 at 19:37









Stéphane Chazelas

282k53521854




282k53521854










asked Nov 27 '17 at 19:20









Andrzej TI

11




11











  • it must be read from a variable.
    – Andrzej TI
    Nov 27 '17 at 19:28
















  • it must be read from a variable.
    – Andrzej TI
    Nov 27 '17 at 19:28















it must be read from a variable.
– Andrzej TI
Nov 27 '17 at 19:28




it must be read from a variable.
– Andrzej TI
Nov 27 '17 at 19:28










3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










This seems to work:



grep -Eo '[0-9]+(.[0-9]+)?' inputfile 


If you're checking the value of a shell variable rather than the contents of a file, you can do this:



echo "$variable" | grep -Eo '[0-9]+(.[0-9]+)?'





share|improve this answer






















  • it must be read from a variable.
    – Andrzej TI
    Nov 27 '17 at 19:29










  • It Rules :) Than You so much :)))
    – Andrzej TI
    Nov 27 '17 at 19:31

















up vote
5
down vote













POSIXly:



n=$variable% bytes # strip the trailing " bytes"
n=$n##*[[:blank:]] # strip the leading part up to the rightmost blank





share|improve this answer



























    up vote
    0
    down vote













    Since you have bash:



    tr -d -c 0-9. <<<$variable


    (would also work in Zsh).






    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%2f407360%2fhow-to-grep-any-digit-from-sentence%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
      1
      down vote



      accepted










      This seems to work:



      grep -Eo '[0-9]+(.[0-9]+)?' inputfile 


      If you're checking the value of a shell variable rather than the contents of a file, you can do this:



      echo "$variable" | grep -Eo '[0-9]+(.[0-9]+)?'





      share|improve this answer






















      • it must be read from a variable.
        – Andrzej TI
        Nov 27 '17 at 19:29










      • It Rules :) Than You so much :)))
        – Andrzej TI
        Nov 27 '17 at 19:31














      up vote
      1
      down vote



      accepted










      This seems to work:



      grep -Eo '[0-9]+(.[0-9]+)?' inputfile 


      If you're checking the value of a shell variable rather than the contents of a file, you can do this:



      echo "$variable" | grep -Eo '[0-9]+(.[0-9]+)?'





      share|improve this answer






















      • it must be read from a variable.
        – Andrzej TI
        Nov 27 '17 at 19:29










      • It Rules :) Than You so much :)))
        – Andrzej TI
        Nov 27 '17 at 19:31












      up vote
      1
      down vote



      accepted







      up vote
      1
      down vote



      accepted






      This seems to work:



      grep -Eo '[0-9]+(.[0-9]+)?' inputfile 


      If you're checking the value of a shell variable rather than the contents of a file, you can do this:



      echo "$variable" | grep -Eo '[0-9]+(.[0-9]+)?'





      share|improve this answer














      This seems to work:



      grep -Eo '[0-9]+(.[0-9]+)?' inputfile 


      If you're checking the value of a shell variable rather than the contents of a file, you can do this:



      echo "$variable" | grep -Eo '[0-9]+(.[0-9]+)?'






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 27 '17 at 19:30

























      answered Nov 27 '17 at 19:25









      DopeGhoti

      40.6k54979




      40.6k54979











      • it must be read from a variable.
        – Andrzej TI
        Nov 27 '17 at 19:29










      • It Rules :) Than You so much :)))
        – Andrzej TI
        Nov 27 '17 at 19:31
















      • it must be read from a variable.
        – Andrzej TI
        Nov 27 '17 at 19:29










      • It Rules :) Than You so much :)))
        – Andrzej TI
        Nov 27 '17 at 19:31















      it must be read from a variable.
      – Andrzej TI
      Nov 27 '17 at 19:29




      it must be read from a variable.
      – Andrzej TI
      Nov 27 '17 at 19:29












      It Rules :) Than You so much :)))
      – Andrzej TI
      Nov 27 '17 at 19:31




      It Rules :) Than You so much :)))
      – Andrzej TI
      Nov 27 '17 at 19:31












      up vote
      5
      down vote













      POSIXly:



      n=$variable% bytes # strip the trailing " bytes"
      n=$n##*[[:blank:]] # strip the leading part up to the rightmost blank





      share|improve this answer
























        up vote
        5
        down vote













        POSIXly:



        n=$variable% bytes # strip the trailing " bytes"
        n=$n##*[[:blank:]] # strip the leading part up to the rightmost blank





        share|improve this answer






















          up vote
          5
          down vote










          up vote
          5
          down vote









          POSIXly:



          n=$variable% bytes # strip the trailing " bytes"
          n=$n##*[[:blank:]] # strip the leading part up to the rightmost blank





          share|improve this answer












          POSIXly:



          n=$variable% bytes # strip the trailing " bytes"
          n=$n##*[[:blank:]] # strip the leading part up to the rightmost blank






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '17 at 19:40









          Stéphane Chazelas

          282k53521854




          282k53521854




















              up vote
              0
              down vote













              Since you have bash:



              tr -d -c 0-9. <<<$variable


              (would also work in Zsh).






              share|improve this answer
























                up vote
                0
                down vote













                Since you have bash:



                tr -d -c 0-9. <<<$variable


                (would also work in Zsh).






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Since you have bash:



                  tr -d -c 0-9. <<<$variable


                  (would also work in Zsh).






                  share|improve this answer












                  Since you have bash:



                  tr -d -c 0-9. <<<$variable


                  (would also work in Zsh).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 28 '17 at 6:12









                  user1934428

                  36119




                  36119



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407360%2fhow-to-grep-any-digit-from-sentence%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?