Grep for a range of numbers in parenthesis

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











up vote
4
down vote

favorite
1












I am trying to grep for only for a range of numbers located inside parenthesis. My current grep will pull everything within the parenthesis but I just want anything from [0001-9999]. How can I fix my grep to do this?



I have tried a few different version of this (?<=()[0001-9999](?=)) but they don't return anything



$ cat /tmp/output
This is R3trans version 6.26 (release 745 - 24.03.17 - 20:17:03).
R3trans finished (0000).
R3trans finished (0001).
R3trans finished (9999).
05.03.2018 16:30:02
enserver, EnqueueServer, RED, Running, 2018 02 27 09:15:52, 151:14:10, 19069


Current output



$ cat /tmp/output | grep -Po '(?<=().*(?=))'
release 745 - 24.03.17 - 20:17:03
0000
0001
9999


Desired output



$ cat /tmp/output | grep -Po '(?<=().*(?=))'
0001
9999






share|improve this question






















  • You want only four digit numbers? Why is [0-9]4 not working? Or, if your RE implementation does not support curly-brace counters, [0-9][0-9][0-9][0-9]?
    – DopeGhoti
    Mar 6 at 17:29











  • That is close but I only want 0001 to 9999 $ cat /tmp/output | grep -Po '(?<=()[0-9]4(?=))' 0000 0001 9999
    – SpruceTips
    Mar 6 at 17:32











  • So take that and strip out 0000 with an inverse search.
    – DopeGhoti
    Mar 6 at 17:37














up vote
4
down vote

favorite
1












I am trying to grep for only for a range of numbers located inside parenthesis. My current grep will pull everything within the parenthesis but I just want anything from [0001-9999]. How can I fix my grep to do this?



I have tried a few different version of this (?<=()[0001-9999](?=)) but they don't return anything



$ cat /tmp/output
This is R3trans version 6.26 (release 745 - 24.03.17 - 20:17:03).
R3trans finished (0000).
R3trans finished (0001).
R3trans finished (9999).
05.03.2018 16:30:02
enserver, EnqueueServer, RED, Running, 2018 02 27 09:15:52, 151:14:10, 19069


Current output



$ cat /tmp/output | grep -Po '(?<=().*(?=))'
release 745 - 24.03.17 - 20:17:03
0000
0001
9999


Desired output



$ cat /tmp/output | grep -Po '(?<=().*(?=))'
0001
9999






share|improve this question






















  • You want only four digit numbers? Why is [0-9]4 not working? Or, if your RE implementation does not support curly-brace counters, [0-9][0-9][0-9][0-9]?
    – DopeGhoti
    Mar 6 at 17:29











  • That is close but I only want 0001 to 9999 $ cat /tmp/output | grep -Po '(?<=()[0-9]4(?=))' 0000 0001 9999
    – SpruceTips
    Mar 6 at 17:32











  • So take that and strip out 0000 with an inverse search.
    – DopeGhoti
    Mar 6 at 17:37












up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





I am trying to grep for only for a range of numbers located inside parenthesis. My current grep will pull everything within the parenthesis but I just want anything from [0001-9999]. How can I fix my grep to do this?



I have tried a few different version of this (?<=()[0001-9999](?=)) but they don't return anything



$ cat /tmp/output
This is R3trans version 6.26 (release 745 - 24.03.17 - 20:17:03).
R3trans finished (0000).
R3trans finished (0001).
R3trans finished (9999).
05.03.2018 16:30:02
enserver, EnqueueServer, RED, Running, 2018 02 27 09:15:52, 151:14:10, 19069


Current output



$ cat /tmp/output | grep -Po '(?<=().*(?=))'
release 745 - 24.03.17 - 20:17:03
0000
0001
9999


Desired output



$ cat /tmp/output | grep -Po '(?<=().*(?=))'
0001
9999






share|improve this question














I am trying to grep for only for a range of numbers located inside parenthesis. My current grep will pull everything within the parenthesis but I just want anything from [0001-9999]. How can I fix my grep to do this?



I have tried a few different version of this (?<=()[0001-9999](?=)) but they don't return anything



$ cat /tmp/output
This is R3trans version 6.26 (release 745 - 24.03.17 - 20:17:03).
R3trans finished (0000).
R3trans finished (0001).
R3trans finished (9999).
05.03.2018 16:30:02
enserver, EnqueueServer, RED, Running, 2018 02 27 09:15:52, 151:14:10, 19069


Current output



$ cat /tmp/output | grep -Po '(?<=().*(?=))'
release 745 - 24.03.17 - 20:17:03
0000
0001
9999


Desired output



$ cat /tmp/output | grep -Po '(?<=().*(?=))'
0001
9999








share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 17:59









ilkkachu

49.2k672136




49.2k672136










asked Mar 6 at 17:27









SpruceTips

170214




170214











  • You want only four digit numbers? Why is [0-9]4 not working? Or, if your RE implementation does not support curly-brace counters, [0-9][0-9][0-9][0-9]?
    – DopeGhoti
    Mar 6 at 17:29











  • That is close but I only want 0001 to 9999 $ cat /tmp/output | grep -Po '(?<=()[0-9]4(?=))' 0000 0001 9999
    – SpruceTips
    Mar 6 at 17:32











  • So take that and strip out 0000 with an inverse search.
    – DopeGhoti
    Mar 6 at 17:37
















  • You want only four digit numbers? Why is [0-9]4 not working? Or, if your RE implementation does not support curly-brace counters, [0-9][0-9][0-9][0-9]?
    – DopeGhoti
    Mar 6 at 17:29











  • That is close but I only want 0001 to 9999 $ cat /tmp/output | grep -Po '(?<=()[0-9]4(?=))' 0000 0001 9999
    – SpruceTips
    Mar 6 at 17:32











  • So take that and strip out 0000 with an inverse search.
    – DopeGhoti
    Mar 6 at 17:37















You want only four digit numbers? Why is [0-9]4 not working? Or, if your RE implementation does not support curly-brace counters, [0-9][0-9][0-9][0-9]?
– DopeGhoti
Mar 6 at 17:29





You want only four digit numbers? Why is [0-9]4 not working? Or, if your RE implementation does not support curly-brace counters, [0-9][0-9][0-9][0-9]?
– DopeGhoti
Mar 6 at 17:29













That is close but I only want 0001 to 9999 $ cat /tmp/output | grep -Po '(?<=()[0-9]4(?=))' 0000 0001 9999
– SpruceTips
Mar 6 at 17:32





That is close but I only want 0001 to 9999 $ cat /tmp/output | grep -Po '(?<=()[0-9]4(?=))' 0000 0001 9999
– SpruceTips
Mar 6 at 17:32













So take that and strip out 0000 with an inverse search.
– DopeGhoti
Mar 6 at 17:37




So take that and strip out 0000 with an inverse search.
– DopeGhoti
Mar 6 at 17:37










3 Answers
3






active

oldest

votes

















up vote
8
down vote



accepted










Two approaches to reach the goal:



grep approach (with Perl support):



grep -Po '(K(?!0000)([0-9]4)(?=))' /tmp/output



GNU awk approach:



awk -v FPAT='\([0-9]4\)' '$1 n = substr($1,2,4); if (int(n) > 0) print n ' /tmp/output



The output:



0001
9999





share|improve this answer





























    up vote
    3
    down vote













    Apparently you have numeric constrains. If this is the case, why not using a programming language? Example:



    perl -nE '/((d4))/ and $1 > 0 and say $1'





    share|improve this answer


















    • 1




      (assuming there's only one occurrence per line)
      – Stéphane Chazelas
      Mar 6 at 17:44

















    up vote
    3
    down vote













    This appears to work:



    grep -Eo '[0-9]4' /path/to/file | grep -v '0000'


    Less readable, but with only one grep:



    grep -Eo '[0-9]3[1-9]|[0-9]2[1-9][0-9]|[0-9][1-9][0-9]2|[1-9][0-9]3' /path/to/file





    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%2f428570%2fgrep-for-a-range-of-numbers-in-parenthesis%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
      8
      down vote



      accepted










      Two approaches to reach the goal:



      grep approach (with Perl support):



      grep -Po '(K(?!0000)([0-9]4)(?=))' /tmp/output



      GNU awk approach:



      awk -v FPAT='\([0-9]4\)' '$1 n = substr($1,2,4); if (int(n) > 0) print n ' /tmp/output



      The output:



      0001
      9999





      share|improve this answer


























        up vote
        8
        down vote



        accepted










        Two approaches to reach the goal:



        grep approach (with Perl support):



        grep -Po '(K(?!0000)([0-9]4)(?=))' /tmp/output



        GNU awk approach:



        awk -v FPAT='\([0-9]4\)' '$1 n = substr($1,2,4); if (int(n) > 0) print n ' /tmp/output



        The output:



        0001
        9999





        share|improve this answer
























          up vote
          8
          down vote



          accepted







          up vote
          8
          down vote



          accepted






          Two approaches to reach the goal:



          grep approach (with Perl support):



          grep -Po '(K(?!0000)([0-9]4)(?=))' /tmp/output



          GNU awk approach:



          awk -v FPAT='\([0-9]4\)' '$1 n = substr($1,2,4); if (int(n) > 0) print n ' /tmp/output



          The output:



          0001
          9999





          share|improve this answer














          Two approaches to reach the goal:



          grep approach (with Perl support):



          grep -Po '(K(?!0000)([0-9]4)(?=))' /tmp/output



          GNU awk approach:



          awk -v FPAT='\([0-9]4\)' '$1 n = substr($1,2,4); if (int(n) > 0) print n ' /tmp/output



          The output:



          0001
          9999






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 6 at 17:44

























          answered Mar 6 at 17:38









          RomanPerekhrest

          22.4k12144




          22.4k12144






















              up vote
              3
              down vote













              Apparently you have numeric constrains. If this is the case, why not using a programming language? Example:



              perl -nE '/((d4))/ and $1 > 0 and say $1'





              share|improve this answer


















              • 1




                (assuming there's only one occurrence per line)
                – Stéphane Chazelas
                Mar 6 at 17:44














              up vote
              3
              down vote













              Apparently you have numeric constrains. If this is the case, why not using a programming language? Example:



              perl -nE '/((d4))/ and $1 > 0 and say $1'





              share|improve this answer


















              • 1




                (assuming there's only one occurrence per line)
                – Stéphane Chazelas
                Mar 6 at 17:44












              up vote
              3
              down vote










              up vote
              3
              down vote









              Apparently you have numeric constrains. If this is the case, why not using a programming language? Example:



              perl -nE '/((d4))/ and $1 > 0 and say $1'





              share|improve this answer














              Apparently you have numeric constrains. If this is the case, why not using a programming language? Example:



              perl -nE '/((d4))/ and $1 > 0 and say $1'






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 6 at 17:44

























              answered Mar 6 at 17:39









              JJoao

              6,7011826




              6,7011826







              • 1




                (assuming there's only one occurrence per line)
                – Stéphane Chazelas
                Mar 6 at 17:44












              • 1




                (assuming there's only one occurrence per line)
                – Stéphane Chazelas
                Mar 6 at 17:44







              1




              1




              (assuming there's only one occurrence per line)
              – Stéphane Chazelas
              Mar 6 at 17:44




              (assuming there's only one occurrence per line)
              – Stéphane Chazelas
              Mar 6 at 17:44










              up vote
              3
              down vote













              This appears to work:



              grep -Eo '[0-9]4' /path/to/file | grep -v '0000'


              Less readable, but with only one grep:



              grep -Eo '[0-9]3[1-9]|[0-9]2[1-9][0-9]|[0-9][1-9][0-9]2|[1-9][0-9]3' /path/to/file





              share|improve this answer


























                up vote
                3
                down vote













                This appears to work:



                grep -Eo '[0-9]4' /path/to/file | grep -v '0000'


                Less readable, but with only one grep:



                grep -Eo '[0-9]3[1-9]|[0-9]2[1-9][0-9]|[0-9][1-9][0-9]2|[1-9][0-9]3' /path/to/file





                share|improve this answer
























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  This appears to work:



                  grep -Eo '[0-9]4' /path/to/file | grep -v '0000'


                  Less readable, but with only one grep:



                  grep -Eo '[0-9]3[1-9]|[0-9]2[1-9][0-9]|[0-9][1-9][0-9]2|[1-9][0-9]3' /path/to/file





                  share|improve this answer














                  This appears to work:



                  grep -Eo '[0-9]4' /path/to/file | grep -v '0000'


                  Less readable, but with only one grep:



                  grep -Eo '[0-9]3[1-9]|[0-9]2[1-9][0-9]|[0-9][1-9][0-9]2|[1-9][0-9]3' /path/to/file






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 7 at 16:00

























                  answered Mar 6 at 17:30









                  DopeGhoti

                  40.2k54779




                  40.2k54779






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428570%2fgrep-for-a-range-of-numbers-in-parenthesis%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?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay