Can not extract the capture group with neither sed nor grep












28















I want to extract the value pair from a key-value pair syntax but I can not.

Example I tried:



echo employee_id=1234 | sed 's/employee_id=([0-9]+)/1/g'


But this gives employee_id=1234 and not 1234 which is actually the capture group.



What am I doing wrong here? I also tried:



echo employee_id=1234| egrep -o employee_id=([0-9]+)


but no sucess.










share|improve this question




















  • 1





    AFAIK sed does not support the '+' quantifier. Instead you have to type the previous item twice: [0-9][0-9]* as anubhava does in his answer.

    – Panos Rontogiannis
    Sep 19 '13 at 11:50






  • 1





    possible duplicate of How to escape plus sign on mac os x (BSD) sed?

    – Michael Foukarakis
    Sep 19 '13 at 14:12











  • echo 'employee_id=1234' | cut -d '=' -f 2

    – ALex_hha
    Mar 11 '16 at 15:53
















28















I want to extract the value pair from a key-value pair syntax but I can not.

Example I tried:



echo employee_id=1234 | sed 's/employee_id=([0-9]+)/1/g'


But this gives employee_id=1234 and not 1234 which is actually the capture group.



What am I doing wrong here? I also tried:



echo employee_id=1234| egrep -o employee_id=([0-9]+)


but no sucess.










share|improve this question




















  • 1





    AFAIK sed does not support the '+' quantifier. Instead you have to type the previous item twice: [0-9][0-9]* as anubhava does in his answer.

    – Panos Rontogiannis
    Sep 19 '13 at 11:50






  • 1





    possible duplicate of How to escape plus sign on mac os x (BSD) sed?

    – Michael Foukarakis
    Sep 19 '13 at 14:12











  • echo 'employee_id=1234' | cut -d '=' -f 2

    – ALex_hha
    Mar 11 '16 at 15:53














28












28








28


11






I want to extract the value pair from a key-value pair syntax but I can not.

Example I tried:



echo employee_id=1234 | sed 's/employee_id=([0-9]+)/1/g'


But this gives employee_id=1234 and not 1234 which is actually the capture group.



What am I doing wrong here? I also tried:



echo employee_id=1234| egrep -o employee_id=([0-9]+)


but no sucess.










share|improve this question
















I want to extract the value pair from a key-value pair syntax but I can not.

Example I tried:



echo employee_id=1234 | sed 's/employee_id=([0-9]+)/1/g'


But this gives employee_id=1234 and not 1234 which is actually the capture group.



What am I doing wrong here? I also tried:



echo employee_id=1234| egrep -o employee_id=([0-9]+)


but no sucess.







regex linux sed grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 19 '13 at 18:41









Adrian Frühwirth

26.1k75163




26.1k75163










asked Sep 19 '13 at 10:52









JimJim

7,5012495179




7,5012495179








  • 1





    AFAIK sed does not support the '+' quantifier. Instead you have to type the previous item twice: [0-9][0-9]* as anubhava does in his answer.

    – Panos Rontogiannis
    Sep 19 '13 at 11:50






  • 1





    possible duplicate of How to escape plus sign on mac os x (BSD) sed?

    – Michael Foukarakis
    Sep 19 '13 at 14:12











  • echo 'employee_id=1234' | cut -d '=' -f 2

    – ALex_hha
    Mar 11 '16 at 15:53














  • 1





    AFAIK sed does not support the '+' quantifier. Instead you have to type the previous item twice: [0-9][0-9]* as anubhava does in his answer.

    – Panos Rontogiannis
    Sep 19 '13 at 11:50






  • 1





    possible duplicate of How to escape plus sign on mac os x (BSD) sed?

    – Michael Foukarakis
    Sep 19 '13 at 14:12











  • echo 'employee_id=1234' | cut -d '=' -f 2

    – ALex_hha
    Mar 11 '16 at 15:53








1




1





AFAIK sed does not support the '+' quantifier. Instead you have to type the previous item twice: [0-9][0-9]* as anubhava does in his answer.

– Panos Rontogiannis
Sep 19 '13 at 11:50





AFAIK sed does not support the '+' quantifier. Instead you have to type the previous item twice: [0-9][0-9]* as anubhava does in his answer.

– Panos Rontogiannis
Sep 19 '13 at 11:50




1




1





possible duplicate of How to escape plus sign on mac os x (BSD) sed?

– Michael Foukarakis
Sep 19 '13 at 14:12





possible duplicate of How to escape plus sign on mac os x (BSD) sed?

– Michael Foukarakis
Sep 19 '13 at 14:12













echo 'employee_id=1234' | cut -d '=' -f 2

– ALex_hha
Mar 11 '16 at 15:53





echo 'employee_id=1234' | cut -d '=' -f 2

– ALex_hha
Mar 11 '16 at 15:53












5 Answers
5






active

oldest

votes


















66














1. Use egrep -o:



echo 'employee_id=1234' | egrep -o '[0-9]+'
1234


2. using grep -oP (PCRE):



echo 'employee_id=1234' | grep -oP 'employee_id=K([0-9]+)'
1234


3. Using sed:



echo 'employee_id=1234' | sed 's/^.*employee_id=([0-9][0-9]*).*$/1/'
1234





share|improve this answer


























  • +1. But why does mine doesn't work. I used egrep -o

    – Jim
    Sep 19 '13 at 10:58











  • Actually this will not work for me as in the same line I am regexing there is one more number. I only want the one after employee_id=

    – Jim
    Sep 19 '13 at 11:00











  • The last command in your comment does not work at all

    – Jim
    Sep 19 '13 at 11:01






  • 1





    None of your answers are relevant. The first I can not use because I need only the number after employee_id=, the second does not work at all and the third one picks the number and if I modify it, it gives what I need but the difference with mine is that you use * in the digits part while I use +. Why does this matter?

    – Jim
    Sep 19 '13 at 11:06






  • 1





    2 worked for me as a way to get PHP version for use in an Ansible playbook php -v | grep -P -o "^PHPsK([0-9]{1}.?[0-9]{0,2}.?[0-9]{0,2})s"

    – turrican_34
    Dec 18 '18 at 14:04



















18














To expand on anubhava's answer number 2, the general pattern to have grep return only the capture group is:



$ regex="$precedes_regexK($capture_regex)(?=$follows_regex)"
$ echo $some_string | grep -oP "$regex"


so



# matches and returns b
$ echo "abc" | grep -oP "aK(b)(?=c)"
b
# no match
$ echo "abc" | grep -oP "zK(b)(?=c)"
# no match
$ echo "abc" | grep -oP "aK(b)(?=d)"





share|improve this answer

































    5














    Using awk



    echo 'employee_id=1234' | awk -F= '{print $2}'
    1234





    share|improve this answer































      3














      You are specifically asking for sed, but in case you may use something else - any POSIX-compliant shell can do parameter expansion which doesn't require a fork/subshell:



      foo='employee_id=1234'
      var=${foo%%=*}
      value=${foo#*=}


       



      $ echo "var=${var} value=${value}"
      var=employee_id value=1234





      share|improve this answer































        1














        use sed -E for extended regex



            echo employee_id=1234 | sed -E 's/employee_id=([0-9]+)/1/g'





        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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%2fstackoverflow.com%2fquestions%2f18892670%2fcan-not-extract-the-capture-group-with-neither-sed-nor-grep%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          66














          1. Use egrep -o:



          echo 'employee_id=1234' | egrep -o '[0-9]+'
          1234


          2. using grep -oP (PCRE):



          echo 'employee_id=1234' | grep -oP 'employee_id=K([0-9]+)'
          1234


          3. Using sed:



          echo 'employee_id=1234' | sed 's/^.*employee_id=([0-9][0-9]*).*$/1/'
          1234





          share|improve this answer


























          • +1. But why does mine doesn't work. I used egrep -o

            – Jim
            Sep 19 '13 at 10:58











          • Actually this will not work for me as in the same line I am regexing there is one more number. I only want the one after employee_id=

            – Jim
            Sep 19 '13 at 11:00











          • The last command in your comment does not work at all

            – Jim
            Sep 19 '13 at 11:01






          • 1





            None of your answers are relevant. The first I can not use because I need only the number after employee_id=, the second does not work at all and the third one picks the number and if I modify it, it gives what I need but the difference with mine is that you use * in the digits part while I use +. Why does this matter?

            – Jim
            Sep 19 '13 at 11:06






          • 1





            2 worked for me as a way to get PHP version for use in an Ansible playbook php -v | grep -P -o "^PHPsK([0-9]{1}.?[0-9]{0,2}.?[0-9]{0,2})s"

            – turrican_34
            Dec 18 '18 at 14:04
















          66














          1. Use egrep -o:



          echo 'employee_id=1234' | egrep -o '[0-9]+'
          1234


          2. using grep -oP (PCRE):



          echo 'employee_id=1234' | grep -oP 'employee_id=K([0-9]+)'
          1234


          3. Using sed:



          echo 'employee_id=1234' | sed 's/^.*employee_id=([0-9][0-9]*).*$/1/'
          1234





          share|improve this answer


























          • +1. But why does mine doesn't work. I used egrep -o

            – Jim
            Sep 19 '13 at 10:58











          • Actually this will not work for me as in the same line I am regexing there is one more number. I only want the one after employee_id=

            – Jim
            Sep 19 '13 at 11:00











          • The last command in your comment does not work at all

            – Jim
            Sep 19 '13 at 11:01






          • 1





            None of your answers are relevant. The first I can not use because I need only the number after employee_id=, the second does not work at all and the third one picks the number and if I modify it, it gives what I need but the difference with mine is that you use * in the digits part while I use +. Why does this matter?

            – Jim
            Sep 19 '13 at 11:06






          • 1





            2 worked for me as a way to get PHP version for use in an Ansible playbook php -v | grep -P -o "^PHPsK([0-9]{1}.?[0-9]{0,2}.?[0-9]{0,2})s"

            – turrican_34
            Dec 18 '18 at 14:04














          66












          66








          66







          1. Use egrep -o:



          echo 'employee_id=1234' | egrep -o '[0-9]+'
          1234


          2. using grep -oP (PCRE):



          echo 'employee_id=1234' | grep -oP 'employee_id=K([0-9]+)'
          1234


          3. Using sed:



          echo 'employee_id=1234' | sed 's/^.*employee_id=([0-9][0-9]*).*$/1/'
          1234





          share|improve this answer















          1. Use egrep -o:



          echo 'employee_id=1234' | egrep -o '[0-9]+'
          1234


          2. using grep -oP (PCRE):



          echo 'employee_id=1234' | grep -oP 'employee_id=K([0-9]+)'
          1234


          3. Using sed:



          echo 'employee_id=1234' | sed 's/^.*employee_id=([0-9][0-9]*).*$/1/'
          1234






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 19 '13 at 11:20

























          answered Sep 19 '13 at 10:56









          anubhavaanubhava

          527k46325402




          527k46325402













          • +1. But why does mine doesn't work. I used egrep -o

            – Jim
            Sep 19 '13 at 10:58











          • Actually this will not work for me as in the same line I am regexing there is one more number. I only want the one after employee_id=

            – Jim
            Sep 19 '13 at 11:00











          • The last command in your comment does not work at all

            – Jim
            Sep 19 '13 at 11:01






          • 1





            None of your answers are relevant. The first I can not use because I need only the number after employee_id=, the second does not work at all and the third one picks the number and if I modify it, it gives what I need but the difference with mine is that you use * in the digits part while I use +. Why does this matter?

            – Jim
            Sep 19 '13 at 11:06






          • 1





            2 worked for me as a way to get PHP version for use in an Ansible playbook php -v | grep -P -o "^PHPsK([0-9]{1}.?[0-9]{0,2}.?[0-9]{0,2})s"

            – turrican_34
            Dec 18 '18 at 14:04



















          • +1. But why does mine doesn't work. I used egrep -o

            – Jim
            Sep 19 '13 at 10:58











          • Actually this will not work for me as in the same line I am regexing there is one more number. I only want the one after employee_id=

            – Jim
            Sep 19 '13 at 11:00











          • The last command in your comment does not work at all

            – Jim
            Sep 19 '13 at 11:01






          • 1





            None of your answers are relevant. The first I can not use because I need only the number after employee_id=, the second does not work at all and the third one picks the number and if I modify it, it gives what I need but the difference with mine is that you use * in the digits part while I use +. Why does this matter?

            – Jim
            Sep 19 '13 at 11:06






          • 1





            2 worked for me as a way to get PHP version for use in an Ansible playbook php -v | grep -P -o "^PHPsK([0-9]{1}.?[0-9]{0,2}.?[0-9]{0,2})s"

            – turrican_34
            Dec 18 '18 at 14:04

















          +1. But why does mine doesn't work. I used egrep -o

          – Jim
          Sep 19 '13 at 10:58





          +1. But why does mine doesn't work. I used egrep -o

          – Jim
          Sep 19 '13 at 10:58













          Actually this will not work for me as in the same line I am regexing there is one more number. I only want the one after employee_id=

          – Jim
          Sep 19 '13 at 11:00





          Actually this will not work for me as in the same line I am regexing there is one more number. I only want the one after employee_id=

          – Jim
          Sep 19 '13 at 11:00













          The last command in your comment does not work at all

          – Jim
          Sep 19 '13 at 11:01





          The last command in your comment does not work at all

          – Jim
          Sep 19 '13 at 11:01




          1




          1





          None of your answers are relevant. The first I can not use because I need only the number after employee_id=, the second does not work at all and the third one picks the number and if I modify it, it gives what I need but the difference with mine is that you use * in the digits part while I use +. Why does this matter?

          – Jim
          Sep 19 '13 at 11:06





          None of your answers are relevant. The first I can not use because I need only the number after employee_id=, the second does not work at all and the third one picks the number and if I modify it, it gives what I need but the difference with mine is that you use * in the digits part while I use +. Why does this matter?

          – Jim
          Sep 19 '13 at 11:06




          1




          1





          2 worked for me as a way to get PHP version for use in an Ansible playbook php -v | grep -P -o "^PHPsK([0-9]{1}.?[0-9]{0,2}.?[0-9]{0,2})s"

          – turrican_34
          Dec 18 '18 at 14:04





          2 worked for me as a way to get PHP version for use in an Ansible playbook php -v | grep -P -o "^PHPsK([0-9]{1}.?[0-9]{0,2}.?[0-9]{0,2})s"

          – turrican_34
          Dec 18 '18 at 14:04













          18














          To expand on anubhava's answer number 2, the general pattern to have grep return only the capture group is:



          $ regex="$precedes_regexK($capture_regex)(?=$follows_regex)"
          $ echo $some_string | grep -oP "$regex"


          so



          # matches and returns b
          $ echo "abc" | grep -oP "aK(b)(?=c)"
          b
          # no match
          $ echo "abc" | grep -oP "zK(b)(?=c)"
          # no match
          $ echo "abc" | grep -oP "aK(b)(?=d)"





          share|improve this answer






























            18














            To expand on anubhava's answer number 2, the general pattern to have grep return only the capture group is:



            $ regex="$precedes_regexK($capture_regex)(?=$follows_regex)"
            $ echo $some_string | grep -oP "$regex"


            so



            # matches and returns b
            $ echo "abc" | grep -oP "aK(b)(?=c)"
            b
            # no match
            $ echo "abc" | grep -oP "zK(b)(?=c)"
            # no match
            $ echo "abc" | grep -oP "aK(b)(?=d)"





            share|improve this answer




























              18












              18








              18







              To expand on anubhava's answer number 2, the general pattern to have grep return only the capture group is:



              $ regex="$precedes_regexK($capture_regex)(?=$follows_regex)"
              $ echo $some_string | grep -oP "$regex"


              so



              # matches and returns b
              $ echo "abc" | grep -oP "aK(b)(?=c)"
              b
              # no match
              $ echo "abc" | grep -oP "zK(b)(?=c)"
              # no match
              $ echo "abc" | grep -oP "aK(b)(?=d)"





              share|improve this answer















              To expand on anubhava's answer number 2, the general pattern to have grep return only the capture group is:



              $ regex="$precedes_regexK($capture_regex)(?=$follows_regex)"
              $ echo $some_string | grep -oP "$regex"


              so



              # matches and returns b
              $ echo "abc" | grep -oP "aK(b)(?=c)"
              b
              # no match
              $ echo "abc" | grep -oP "zK(b)(?=c)"
              # no match
              $ echo "abc" | grep -oP "aK(b)(?=d)"






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited May 12 '16 at 22:25

























              answered Apr 6 '16 at 15:34









              jayflojayflo

              5401619




              5401619























                  5














                  Using awk



                  echo 'employee_id=1234' | awk -F= '{print $2}'
                  1234





                  share|improve this answer




























                    5














                    Using awk



                    echo 'employee_id=1234' | awk -F= '{print $2}'
                    1234





                    share|improve this answer


























                      5












                      5








                      5







                      Using awk



                      echo 'employee_id=1234' | awk -F= '{print $2}'
                      1234





                      share|improve this answer













                      Using awk



                      echo 'employee_id=1234' | awk -F= '{print $2}'
                      1234






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 19 '13 at 11:29









                      JotneJotne

                      29.6k83245




                      29.6k83245























                          3














                          You are specifically asking for sed, but in case you may use something else - any POSIX-compliant shell can do parameter expansion which doesn't require a fork/subshell:



                          foo='employee_id=1234'
                          var=${foo%%=*}
                          value=${foo#*=}


                           



                          $ echo "var=${var} value=${value}"
                          var=employee_id value=1234





                          share|improve this answer




























                            3














                            You are specifically asking for sed, but in case you may use something else - any POSIX-compliant shell can do parameter expansion which doesn't require a fork/subshell:



                            foo='employee_id=1234'
                            var=${foo%%=*}
                            value=${foo#*=}


                             



                            $ echo "var=${var} value=${value}"
                            var=employee_id value=1234





                            share|improve this answer


























                              3












                              3








                              3







                              You are specifically asking for sed, but in case you may use something else - any POSIX-compliant shell can do parameter expansion which doesn't require a fork/subshell:



                              foo='employee_id=1234'
                              var=${foo%%=*}
                              value=${foo#*=}


                               



                              $ echo "var=${var} value=${value}"
                              var=employee_id value=1234





                              share|improve this answer













                              You are specifically asking for sed, but in case you may use something else - any POSIX-compliant shell can do parameter expansion which doesn't require a fork/subshell:



                              foo='employee_id=1234'
                              var=${foo%%=*}
                              value=${foo#*=}


                               



                              $ echo "var=${var} value=${value}"
                              var=employee_id value=1234






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Sep 19 '13 at 14:00









                              Adrian FrühwirthAdrian Frühwirth

                              26.1k75163




                              26.1k75163























                                  1














                                  use sed -E for extended regex



                                      echo employee_id=1234 | sed -E 's/employee_id=([0-9]+)/1/g'





                                  share|improve this answer




























                                    1














                                    use sed -E for extended regex



                                        echo employee_id=1234 | sed -E 's/employee_id=([0-9]+)/1/g'





                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      use sed -E for extended regex



                                          echo employee_id=1234 | sed -E 's/employee_id=([0-9]+)/1/g'





                                      share|improve this answer













                                      use sed -E for extended regex



                                          echo employee_id=1234 | sed -E 's/employee_id=([0-9]+)/1/g'






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 20 '18 at 5:34









                                      commander Ghostcommander Ghost

                                      111




                                      111






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • 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%2fstackoverflow.com%2fquestions%2f18892670%2fcan-not-extract-the-capture-group-with-neither-sed-nor-grep%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

                                          Guess what letter conforming each word

                                          Port of Spain

                                          Run scheduled task as local user group (not BUILTIN)