Grep only numbers, not the alphanumeric entries











up vote
0
down vote

favorite












I have a list of values like:



1
2
3
4
Ak123
Ak23
Ak147
1Apple
2Apricot
3Mango
4Orange


I just want to execute a simple grep command to list me only the numbers. i.e. 1, 2 , 3 and 4.
I tried this command -



grep -Ein --color '^s*[0-9]' test.txt


but it returns the alphanumeric also. Instead I tried omitting the character by this command:



grep -Ein --color '^s*[0-9][^A-Z]' test.txt


but it gives 0 results.










share|improve this question









New contributor




Srinivasan Senthil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    I have a list of values like:



    1
    2
    3
    4
    Ak123
    Ak23
    Ak147
    1Apple
    2Apricot
    3Mango
    4Orange


    I just want to execute a simple grep command to list me only the numbers. i.e. 1, 2 , 3 and 4.
    I tried this command -



    grep -Ein --color '^s*[0-9]' test.txt


    but it returns the alphanumeric also. Instead I tried omitting the character by this command:



    grep -Ein --color '^s*[0-9][^A-Z]' test.txt


    but it gives 0 results.










    share|improve this question









    New contributor




    Srinivasan Senthil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a list of values like:



      1
      2
      3
      4
      Ak123
      Ak23
      Ak147
      1Apple
      2Apricot
      3Mango
      4Orange


      I just want to execute a simple grep command to list me only the numbers. i.e. 1, 2 , 3 and 4.
      I tried this command -



      grep -Ein --color '^s*[0-9]' test.txt


      but it returns the alphanumeric also. Instead I tried omitting the character by this command:



      grep -Ein --color '^s*[0-9][^A-Z]' test.txt


      but it gives 0 results.










      share|improve this question









      New contributor




      Srinivasan Senthil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have a list of values like:



      1
      2
      3
      4
      Ak123
      Ak23
      Ak147
      1Apple
      2Apricot
      3Mango
      4Orange


      I just want to execute a simple grep command to list me only the numbers. i.e. 1, 2 , 3 and 4.
      I tried this command -



      grep -Ein --color '^s*[0-9]' test.txt


      but it returns the alphanumeric also. Instead I tried omitting the character by this command:



      grep -Ein --color '^s*[0-9][^A-Z]' test.txt


      but it gives 0 results.







      grep regular-expression numeric-data






      share|improve this question









      New contributor




      Srinivasan Senthil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Srinivasan Senthil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 29 at 18:36









      Kusalananda

      118k16223362




      118k16223362






      New contributor




      Srinivasan Senthil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 29 at 18:01









      Srinivasan Senthil

      1




      1




      New contributor




      Srinivasan Senthil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Srinivasan Senthil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Srinivasan Senthil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote













          Assuming each line only contains a single word with no flanking whitespace:



          grep -x -E '[0-9]+'


          or



          grep -x -E '[[:digit:]]+'


          This would extract any line that contained only digits. The -x option to grep forces the pattern to match across a complete line. I'm using -E to enable extended regular expression to be able to use +.



          The pattern [[:digit:]]+ would match at least one digit. The type of digit that it matches may depend on your locale.



          To allow for whitespace before and after:



          grep -x -E '[[:blank:]]*[0-9]+[[:blank:]]*'


          or



          grep -x -E '[[:blank:]]*[[:digit:]]+[[:blank:]]*'


          The pattern [[:blank:]]* matches zero or more tabs or spaces.





          Your expression ^s*[0-9] matches lines that may start with a space character (assuming s matches a space character, it's really a PCRE) and then has a digit. After the digit, any character may occur, as in the string 2Apricot.



          Your expression ^s*[0-9][^A-Z] is similar, but [^A-Z] forces the matching of a second non-alphabetic non-uppercase character. This matches things like 33, 1a, and 5- and longer strings, but would not match the single character string 3.






          share|improve this answer























          • Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
            – Srinivasan Senthil
            Nov 29 at 18:28












          • @SrinivasanSenthil You mean -x (lowercase). As I wrote, it forces the match to span the whole line. It's like including ^ at the start and $ at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.
            – Kusalananda
            Nov 29 at 18:34


















          up vote
          1
          down vote













          Try also



          $ grep -v '[^ 0-9]' file
          1
          2
          3
          4





          share|improve this answer





















          • I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
            – glenn jackman
            Nov 30 at 16:31











          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: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          });


          }
          });






          Srinivasan Senthil is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484991%2fgrep-only-numbers-not-the-alphanumeric-entries%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








          up vote
          5
          down vote













          Assuming each line only contains a single word with no flanking whitespace:



          grep -x -E '[0-9]+'


          or



          grep -x -E '[[:digit:]]+'


          This would extract any line that contained only digits. The -x option to grep forces the pattern to match across a complete line. I'm using -E to enable extended regular expression to be able to use +.



          The pattern [[:digit:]]+ would match at least one digit. The type of digit that it matches may depend on your locale.



          To allow for whitespace before and after:



          grep -x -E '[[:blank:]]*[0-9]+[[:blank:]]*'


          or



          grep -x -E '[[:blank:]]*[[:digit:]]+[[:blank:]]*'


          The pattern [[:blank:]]* matches zero or more tabs or spaces.





          Your expression ^s*[0-9] matches lines that may start with a space character (assuming s matches a space character, it's really a PCRE) and then has a digit. After the digit, any character may occur, as in the string 2Apricot.



          Your expression ^s*[0-9][^A-Z] is similar, but [^A-Z] forces the matching of a second non-alphabetic non-uppercase character. This matches things like 33, 1a, and 5- and longer strings, but would not match the single character string 3.






          share|improve this answer























          • Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
            – Srinivasan Senthil
            Nov 29 at 18:28












          • @SrinivasanSenthil You mean -x (lowercase). As I wrote, it forces the match to span the whole line. It's like including ^ at the start and $ at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.
            – Kusalananda
            Nov 29 at 18:34















          up vote
          5
          down vote













          Assuming each line only contains a single word with no flanking whitespace:



          grep -x -E '[0-9]+'


          or



          grep -x -E '[[:digit:]]+'


          This would extract any line that contained only digits. The -x option to grep forces the pattern to match across a complete line. I'm using -E to enable extended regular expression to be able to use +.



          The pattern [[:digit:]]+ would match at least one digit. The type of digit that it matches may depend on your locale.



          To allow for whitespace before and after:



          grep -x -E '[[:blank:]]*[0-9]+[[:blank:]]*'


          or



          grep -x -E '[[:blank:]]*[[:digit:]]+[[:blank:]]*'


          The pattern [[:blank:]]* matches zero or more tabs or spaces.





          Your expression ^s*[0-9] matches lines that may start with a space character (assuming s matches a space character, it's really a PCRE) and then has a digit. After the digit, any character may occur, as in the string 2Apricot.



          Your expression ^s*[0-9][^A-Z] is similar, but [^A-Z] forces the matching of a second non-alphabetic non-uppercase character. This matches things like 33, 1a, and 5- and longer strings, but would not match the single character string 3.






          share|improve this answer























          • Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
            – Srinivasan Senthil
            Nov 29 at 18:28












          • @SrinivasanSenthil You mean -x (lowercase). As I wrote, it forces the match to span the whole line. It's like including ^ at the start and $ at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.
            – Kusalananda
            Nov 29 at 18:34













          up vote
          5
          down vote










          up vote
          5
          down vote









          Assuming each line only contains a single word with no flanking whitespace:



          grep -x -E '[0-9]+'


          or



          grep -x -E '[[:digit:]]+'


          This would extract any line that contained only digits. The -x option to grep forces the pattern to match across a complete line. I'm using -E to enable extended regular expression to be able to use +.



          The pattern [[:digit:]]+ would match at least one digit. The type of digit that it matches may depend on your locale.



          To allow for whitespace before and after:



          grep -x -E '[[:blank:]]*[0-9]+[[:blank:]]*'


          or



          grep -x -E '[[:blank:]]*[[:digit:]]+[[:blank:]]*'


          The pattern [[:blank:]]* matches zero or more tabs or spaces.





          Your expression ^s*[0-9] matches lines that may start with a space character (assuming s matches a space character, it's really a PCRE) and then has a digit. After the digit, any character may occur, as in the string 2Apricot.



          Your expression ^s*[0-9][^A-Z] is similar, but [^A-Z] forces the matching of a second non-alphabetic non-uppercase character. This matches things like 33, 1a, and 5- and longer strings, but would not match the single character string 3.






          share|improve this answer














          Assuming each line only contains a single word with no flanking whitespace:



          grep -x -E '[0-9]+'


          or



          grep -x -E '[[:digit:]]+'


          This would extract any line that contained only digits. The -x option to grep forces the pattern to match across a complete line. I'm using -E to enable extended regular expression to be able to use +.



          The pattern [[:digit:]]+ would match at least one digit. The type of digit that it matches may depend on your locale.



          To allow for whitespace before and after:



          grep -x -E '[[:blank:]]*[0-9]+[[:blank:]]*'


          or



          grep -x -E '[[:blank:]]*[[:digit:]]+[[:blank:]]*'


          The pattern [[:blank:]]* matches zero or more tabs or spaces.





          Your expression ^s*[0-9] matches lines that may start with a space character (assuming s matches a space character, it's really a PCRE) and then has a digit. After the digit, any character may occur, as in the string 2Apricot.



          Your expression ^s*[0-9][^A-Z] is similar, but [^A-Z] forces the matching of a second non-alphabetic non-uppercase character. This matches things like 33, 1a, and 5- and longer strings, but would not match the single character string 3.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 29 at 18:26

























          answered Nov 29 at 18:07









          Kusalananda

          118k16223362




          118k16223362












          • Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
            – Srinivasan Senthil
            Nov 29 at 18:28












          • @SrinivasanSenthil You mean -x (lowercase). As I wrote, it forces the match to span the whole line. It's like including ^ at the start and $ at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.
            – Kusalananda
            Nov 29 at 18:34


















          • Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
            – Srinivasan Senthil
            Nov 29 at 18:28












          • @SrinivasanSenthil You mean -x (lowercase). As I wrote, it forces the match to span the whole line. It's like including ^ at the start and $ at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.
            – Kusalananda
            Nov 29 at 18:34
















          Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
          – Srinivasan Senthil
          Nov 29 at 18:28






          Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
          – Srinivasan Senthil
          Nov 29 at 18:28














          @SrinivasanSenthil You mean -x (lowercase). As I wrote, it forces the match to span the whole line. It's like including ^ at the start and $ at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.
          – Kusalananda
          Nov 29 at 18:34




          @SrinivasanSenthil You mean -x (lowercase). As I wrote, it forces the match to span the whole line. It's like including ^ at the start and $ at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.
          – Kusalananda
          Nov 29 at 18:34












          up vote
          1
          down vote













          Try also



          $ grep -v '[^ 0-9]' file
          1
          2
          3
          4





          share|improve this answer





















          • I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
            – glenn jackman
            Nov 30 at 16:31















          up vote
          1
          down vote













          Try also



          $ grep -v '[^ 0-9]' file
          1
          2
          3
          4





          share|improve this answer





















          • I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
            – glenn jackman
            Nov 30 at 16:31













          up vote
          1
          down vote










          up vote
          1
          down vote









          Try also



          $ grep -v '[^ 0-9]' file
          1
          2
          3
          4





          share|improve this answer












          Try also



          $ grep -v '[^ 0-9]' file
          1
          2
          3
          4






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 29 at 21:48









          RudiC

          3,5321312




          3,5321312












          • I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
            – glenn jackman
            Nov 30 at 16:31


















          • I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
            – glenn jackman
            Nov 30 at 16:31
















          I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
          – glenn jackman
          Nov 30 at 16:31




          I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
          – glenn jackman
          Nov 30 at 16:31










          Srinivasan Senthil is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Srinivasan Senthil is a new contributor. Be nice, and check out our Code of Conduct.













          Srinivasan Senthil is a new contributor. Be nice, and check out our Code of Conduct.












          Srinivasan Senthil is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • 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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2funix.stackexchange.com%2fquestions%2f484991%2fgrep-only-numbers-not-the-alphanumeric-entries%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

          Entries order in /etc/network/interfaces

          新発田市

          Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)