grep - Get word from string












0















I have a bunch of strings that I have to fetch the 'port_num' from -



"76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0"


The word might be in a different place in the string and it might be a different length, but it always says 'port_num=' before it and ';' after it...



I only want this bit- 'switch01'



Currently I use-



| grep -Eo 'port_num=.+' | cut -d"=" -f2 | cut -d";" -f1'


But there has got to be a better way










share|improve this question





























    0















    I have a bunch of strings that I have to fetch the 'port_num' from -



    "76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0"


    The word might be in a different place in the string and it might be a different length, but it always says 'port_num=' before it and ';' after it...



    I only want this bit- 'switch01'



    Currently I use-



    | grep -Eo 'port_num=.+' | cut -d"=" -f2 | cut -d";" -f1'


    But there has got to be a better way










    share|improve this question



























      0












      0








      0








      I have a bunch of strings that I have to fetch the 'port_num' from -



      "76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0"


      The word might be in a different place in the string and it might be a different length, but it always says 'port_num=' before it and ';' after it...



      I only want this bit- 'switch01'



      Currently I use-



      | grep -Eo 'port_num=.+' | cut -d"=" -f2 | cut -d";" -f1'


      But there has got to be a better way










      share|improve this question
















      I have a bunch of strings that I have to fetch the 'port_num' from -



      "76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0"


      The word might be in a different place in the string and it might be a different length, but it always says 'port_num=' before it and ';' after it...



      I only want this bit- 'switch01'



      Currently I use-



      | grep -Eo 'port_num=.+' | cut -d"=" -f2 | cut -d";" -f1'


      But there has got to be a better way







      grep






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 14:29









      Inian

      39k63770




      39k63770










      asked Nov 12 '18 at 14:09









      BrickBrick

      586




      586
























          4 Answers
          4






          active

          oldest

          votes


















          1














          You can try grep -oP '(?<=port_num=).+(?=;)', if you run this:



          echo "76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0" 
          | grep -oP '(?<=port_num=).+(?=;)'


          result will be:



          switch01


          Updated answer: grep -oP '(?<=port_num=)[^;]+(?=;)'






          share|improve this answer





















          • 1





            Perfect! thanks

            – Brick
            Nov 13 '18 at 16:04











          • Spoke to soon! sometimes there is a second ';' after the first... and it seems to continue until the last ';' in the line - any ideas?

            – Brick
            Nov 13 '18 at 16:21











          • grep -oP '(?<=port_num=)[^;]+(?=;)'

            – Vladimir Kovpak
            Nov 13 '18 at 16:49



















          1














          This is what I would use:



          ... | grep -E 'port_num=.+' | sed 's/^.*port_num=([^;]*).*$/1/'


          This works with or without the -o on grep, and the availability of -P will depend on the version of grep you have. (e.g., my grep does not have it). I'm not saying the other answers that rely on -P aren't any good -- they look fine to me. But grep -P will be less portable.



          IMHO, piping grep with sed allows each utility to do what it specializes in -- grep is for selecting lines, sed is for modifying lines.






          share|improve this answer

































            1














            This can be done in a simple sed command:



            s="76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0"
            sed 's/.*port_num=([^;]*);.*/1/' <<< "$s"




            switch01





            share|improve this answer































              1














              ... | grep -Po 'port_num.+(?=;)'


              This uses grep's Perl Compatible Regular Expression (PCRE) syntax. The (?=;) is a look-ahead assertion which looks for a match with ";" but doesn't include it in the matched output.



              This produces:
              port_num=switch01



              As @Vladimir Kovpak noted, if you want to exclude the "port_num=" string from this output, add a look-behind assertion:



              ... | grep -Po '(?<=port_num).+(?=;)'





              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%2f53263903%2fgrep-get-word-from-string%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                You can try grep -oP '(?<=port_num=).+(?=;)', if you run this:



                echo "76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0" 
                | grep -oP '(?<=port_num=).+(?=;)'


                result will be:



                switch01


                Updated answer: grep -oP '(?<=port_num=)[^;]+(?=;)'






                share|improve this answer





















                • 1





                  Perfect! thanks

                  – Brick
                  Nov 13 '18 at 16:04











                • Spoke to soon! sometimes there is a second ';' after the first... and it seems to continue until the last ';' in the line - any ideas?

                  – Brick
                  Nov 13 '18 at 16:21











                • grep -oP '(?<=port_num=)[^;]+(?=;)'

                  – Vladimir Kovpak
                  Nov 13 '18 at 16:49
















                1














                You can try grep -oP '(?<=port_num=).+(?=;)', if you run this:



                echo "76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0" 
                | grep -oP '(?<=port_num=).+(?=;)'


                result will be:



                switch01


                Updated answer: grep -oP '(?<=port_num=)[^;]+(?=;)'






                share|improve this answer





















                • 1





                  Perfect! thanks

                  – Brick
                  Nov 13 '18 at 16:04











                • Spoke to soon! sometimes there is a second ';' after the first... and it seems to continue until the last ';' in the line - any ideas?

                  – Brick
                  Nov 13 '18 at 16:21











                • grep -oP '(?<=port_num=)[^;]+(?=;)'

                  – Vladimir Kovpak
                  Nov 13 '18 at 16:49














                1












                1








                1







                You can try grep -oP '(?<=port_num=).+(?=;)', if you run this:



                echo "76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0" 
                | grep -oP '(?<=port_num=).+(?=;)'


                result will be:



                switch01


                Updated answer: grep -oP '(?<=port_num=)[^;]+(?=;)'






                share|improve this answer















                You can try grep -oP '(?<=port_num=).+(?=;)', if you run this:



                echo "76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0" 
                | grep -oP '(?<=port_num=).+(?=;)'


                result will be:



                switch01


                Updated answer: grep -oP '(?<=port_num=)[^;]+(?=;)'







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 '18 at 16:52

























                answered Nov 12 '18 at 14:21









                Vladimir KovpakVladimir Kovpak

                10.6k43646




                10.6k43646








                • 1





                  Perfect! thanks

                  – Brick
                  Nov 13 '18 at 16:04











                • Spoke to soon! sometimes there is a second ';' after the first... and it seems to continue until the last ';' in the line - any ideas?

                  – Brick
                  Nov 13 '18 at 16:21











                • grep -oP '(?<=port_num=)[^;]+(?=;)'

                  – Vladimir Kovpak
                  Nov 13 '18 at 16:49














                • 1





                  Perfect! thanks

                  – Brick
                  Nov 13 '18 at 16:04











                • Spoke to soon! sometimes there is a second ';' after the first... and it seems to continue until the last ';' in the line - any ideas?

                  – Brick
                  Nov 13 '18 at 16:21











                • grep -oP '(?<=port_num=)[^;]+(?=;)'

                  – Vladimir Kovpak
                  Nov 13 '18 at 16:49








                1




                1





                Perfect! thanks

                – Brick
                Nov 13 '18 at 16:04





                Perfect! thanks

                – Brick
                Nov 13 '18 at 16:04













                Spoke to soon! sometimes there is a second ';' after the first... and it seems to continue until the last ';' in the line - any ideas?

                – Brick
                Nov 13 '18 at 16:21





                Spoke to soon! sometimes there is a second ';' after the first... and it seems to continue until the last ';' in the line - any ideas?

                – Brick
                Nov 13 '18 at 16:21













                grep -oP '(?<=port_num=)[^;]+(?=;)'

                – Vladimir Kovpak
                Nov 13 '18 at 16:49





                grep -oP '(?<=port_num=)[^;]+(?=;)'

                – Vladimir Kovpak
                Nov 13 '18 at 16:49













                1














                This is what I would use:



                ... | grep -E 'port_num=.+' | sed 's/^.*port_num=([^;]*).*$/1/'


                This works with or without the -o on grep, and the availability of -P will depend on the version of grep you have. (e.g., my grep does not have it). I'm not saying the other answers that rely on -P aren't any good -- they look fine to me. But grep -P will be less portable.



                IMHO, piping grep with sed allows each utility to do what it specializes in -- grep is for selecting lines, sed is for modifying lines.






                share|improve this answer






























                  1














                  This is what I would use:



                  ... | grep -E 'port_num=.+' | sed 's/^.*port_num=([^;]*).*$/1/'


                  This works with or without the -o on grep, and the availability of -P will depend on the version of grep you have. (e.g., my grep does not have it). I'm not saying the other answers that rely on -P aren't any good -- they look fine to me. But grep -P will be less portable.



                  IMHO, piping grep with sed allows each utility to do what it specializes in -- grep is for selecting lines, sed is for modifying lines.






                  share|improve this answer




























                    1












                    1








                    1







                    This is what I would use:



                    ... | grep -E 'port_num=.+' | sed 's/^.*port_num=([^;]*).*$/1/'


                    This works with or without the -o on grep, and the availability of -P will depend on the version of grep you have. (e.g., my grep does not have it). I'm not saying the other answers that rely on -P aren't any good -- they look fine to me. But grep -P will be less portable.



                    IMHO, piping grep with sed allows each utility to do what it specializes in -- grep is for selecting lines, sed is for modifying lines.






                    share|improve this answer















                    This is what I would use:



                    ... | grep -E 'port_num=.+' | sed 's/^.*port_num=([^;]*).*$/1/'


                    This works with or without the -o on grep, and the availability of -P will depend on the version of grep you have. (e.g., my grep does not have it). I'm not saying the other answers that rely on -P aren't any good -- they look fine to me. But grep -P will be less portable.



                    IMHO, piping grep with sed allows each utility to do what it specializes in -- grep is for selecting lines, sed is for modifying lines.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 12 '18 at 14:36

























                    answered Nov 12 '18 at 14:15









                    landru27landru27

                    775213




                    775213























                        1














                        This can be done in a simple sed command:



                        s="76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0"
                        sed 's/.*port_num=([^;]*);.*/1/' <<< "$s"




                        switch01





                        share|improve this answer




























                          1














                          This can be done in a simple sed command:



                          s="76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0"
                          sed 's/.*port_num=([^;]*);.*/1/' <<< "$s"




                          switch01





                          share|improve this answer


























                            1












                            1








                            1







                            This can be done in a simple sed command:



                            s="76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0"
                            sed 's/.*port_num=([^;]*);.*/1/' <<< "$s"




                            switch01





                            share|improve this answer













                            This can be done in a simple sed command:



                            s="76 : client=new; tags=circ, LINK; port_num=switch01; far_port=Gi1/0"
                            sed 's/.*port_num=([^;]*);.*/1/' <<< "$s"




                            switch01






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 '18 at 15:14









                            anubhavaanubhava

                            521k46317391




                            521k46317391























                                1














                                ... | grep -Po 'port_num.+(?=;)'


                                This uses grep's Perl Compatible Regular Expression (PCRE) syntax. The (?=;) is a look-ahead assertion which looks for a match with ";" but doesn't include it in the matched output.



                                This produces:
                                port_num=switch01



                                As @Vladimir Kovpak noted, if you want to exclude the "port_num=" string from this output, add a look-behind assertion:



                                ... | grep -Po '(?<=port_num).+(?=;)'





                                share|improve this answer






























                                  1














                                  ... | grep -Po 'port_num.+(?=;)'


                                  This uses grep's Perl Compatible Regular Expression (PCRE) syntax. The (?=;) is a look-ahead assertion which looks for a match with ";" but doesn't include it in the matched output.



                                  This produces:
                                  port_num=switch01



                                  As @Vladimir Kovpak noted, if you want to exclude the "port_num=" string from this output, add a look-behind assertion:



                                  ... | grep -Po '(?<=port_num).+(?=;)'





                                  share|improve this answer




























                                    1












                                    1








                                    1







                                    ... | grep -Po 'port_num.+(?=;)'


                                    This uses grep's Perl Compatible Regular Expression (PCRE) syntax. The (?=;) is a look-ahead assertion which looks for a match with ";" but doesn't include it in the matched output.



                                    This produces:
                                    port_num=switch01



                                    As @Vladimir Kovpak noted, if you want to exclude the "port_num=" string from this output, add a look-behind assertion:



                                    ... | grep -Po '(?<=port_num).+(?=;)'





                                    share|improve this answer















                                    ... | grep -Po 'port_num.+(?=;)'


                                    This uses grep's Perl Compatible Regular Expression (PCRE) syntax. The (?=;) is a look-ahead assertion which looks for a match with ";" but doesn't include it in the matched output.



                                    This produces:
                                    port_num=switch01



                                    As @Vladimir Kovpak noted, if you want to exclude the "port_num=" string from this output, add a look-behind assertion:



                                    ... | grep -Po '(?<=port_num).+(?=;)'






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 12 '18 at 15:28

























                                    answered Nov 12 '18 at 14:23









                                    JRFergusonJRFerguson

                                    5,99212231




                                    5,99212231






























                                        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%2f53263903%2fgrep-get-word-from-string%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

                                        Full-time equivalent

                                        さくらももこ

                                        13 indicted, 8 arrested in Calif. drug cartel investigation