bash + find specific word in space-delimited string












1















The parameter list include the following example values



# echo $list
master01.montil131.com worker01.montil131.com worker02.montil131.com


In order to find the word that include master string I do the following



list=$( for i in ` echo  $list `; do [[ $i =~ master ]] && echo $i ; done )

echo $list
master01.montil131.com


but this approach to find the string with master word isn't elegant way



Any other suggestion how to find specific word in list ?










share|improve this question

























  • is $list a variable that contains the string "master01.montil131.com worker01.montil131.com worker02.montil131.com" ? a space separated list of words stored in a variable?

    – glenn jackman
    7 hours ago











  • just one space between words ( no other comma separator )'

    – yael
    7 hours ago











  • Unrelated: Why do you store a list in a string? It would be better to have it in an array...

    – Kusalananda
    1 hour ago
















1















The parameter list include the following example values



# echo $list
master01.montil131.com worker01.montil131.com worker02.montil131.com


In order to find the word that include master string I do the following



list=$( for i in ` echo  $list `; do [[ $i =~ master ]] && echo $i ; done )

echo $list
master01.montil131.com


but this approach to find the string with master word isn't elegant way



Any other suggestion how to find specific word in list ?










share|improve this question

























  • is $list a variable that contains the string "master01.montil131.com worker01.montil131.com worker02.montil131.com" ? a space separated list of words stored in a variable?

    – glenn jackman
    7 hours ago











  • just one space between words ( no other comma separator )'

    – yael
    7 hours ago











  • Unrelated: Why do you store a list in a string? It would be better to have it in an array...

    – Kusalananda
    1 hour ago














1












1








1








The parameter list include the following example values



# echo $list
master01.montil131.com worker01.montil131.com worker02.montil131.com


In order to find the word that include master string I do the following



list=$( for i in ` echo  $list `; do [[ $i =~ master ]] && echo $i ; done )

echo $list
master01.montil131.com


but this approach to find the string with master word isn't elegant way



Any other suggestion how to find specific word in list ?










share|improve this question
















The parameter list include the following example values



# echo $list
master01.montil131.com worker01.montil131.com worker02.montil131.com


In order to find the word that include master string I do the following



list=$( for i in ` echo  $list `; do [[ $i =~ master ]] && echo $i ; done )

echo $list
master01.montil131.com


but this approach to find the string with master word isn't elegant way



Any other suggestion how to find specific word in list ?







linux bash shell-script awk sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









Kusalananda

131k17249408




131k17249408










asked 7 hours ago









yaelyael

2,58712570




2,58712570













  • is $list a variable that contains the string "master01.montil131.com worker01.montil131.com worker02.montil131.com" ? a space separated list of words stored in a variable?

    – glenn jackman
    7 hours ago











  • just one space between words ( no other comma separator )'

    – yael
    7 hours ago











  • Unrelated: Why do you store a list in a string? It would be better to have it in an array...

    – Kusalananda
    1 hour ago



















  • is $list a variable that contains the string "master01.montil131.com worker01.montil131.com worker02.montil131.com" ? a space separated list of words stored in a variable?

    – glenn jackman
    7 hours ago











  • just one space between words ( no other comma separator )'

    – yael
    7 hours ago











  • Unrelated: Why do you store a list in a string? It would be better to have it in an array...

    – Kusalananda
    1 hour ago

















is $list a variable that contains the string "master01.montil131.com worker01.montil131.com worker02.montil131.com" ? a space separated list of words stored in a variable?

– glenn jackman
7 hours ago





is $list a variable that contains the string "master01.montil131.com worker01.montil131.com worker02.montil131.com" ? a space separated list of words stored in a variable?

– glenn jackman
7 hours ago













just one space between words ( no other comma separator )'

– yael
7 hours ago





just one space between words ( no other comma separator )'

– yael
7 hours ago













Unrelated: Why do you store a list in a string? It would be better to have it in an array...

– Kusalananda
1 hour ago





Unrelated: Why do you store a list in a string? It would be better to have it in an array...

– Kusalananda
1 hour ago










3 Answers
3






active

oldest

votes


















1














Why not use simple grep command, I am assuming there are no spaces in a word in the list.



 echo $list | tr ' ' 'n' | grep master


It will replace space with new line and will then grep word master.






share|improve this answer

































    2














    Use grep -o:



    grep -o "[^ ]*master[^ ]*" <<<"$list"


    If you know that you always have just master* and worker*, you can use Shell methods:



    echo "${list// *worker[^ ]*/}"





    share|improve this answer

































      0














      grep -ow 'master[^ ]*' <<<"$list"


      or, with GNU grep,



      grep -Pow 'masterS+' <<<"$list"


      The -o would extract the matching bit of the string in $list, and -w would ensure that we don't match themaster or some other word that does not start with master.



      The S in the second command is a PCRE that will match any non-space character.






      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',
        autoActivateHeartbeat: false,
        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
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501609%2fbash-find-specific-word-in-space-delimited-string%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Why not use simple grep command, I am assuming there are no spaces in a word in the list.



         echo $list | tr ' ' 'n' | grep master


        It will replace space with new line and will then grep word master.






        share|improve this answer






























          1














          Why not use simple grep command, I am assuming there are no spaces in a word in the list.



           echo $list | tr ' ' 'n' | grep master


          It will replace space with new line and will then grep word master.






          share|improve this answer




























            1












            1








            1







            Why not use simple grep command, I am assuming there are no spaces in a word in the list.



             echo $list | tr ' ' 'n' | grep master


            It will replace space with new line and will then grep word master.






            share|improve this answer















            Why not use simple grep command, I am assuming there are no spaces in a word in the list.



             echo $list | tr ' ' 'n' | grep master


            It will replace space with new line and will then grep word master.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 7 hours ago

























            answered 7 hours ago









            PRYPRY

            2,43031025




            2,43031025

























                2














                Use grep -o:



                grep -o "[^ ]*master[^ ]*" <<<"$list"


                If you know that you always have just master* and worker*, you can use Shell methods:



                echo "${list// *worker[^ ]*/}"





                share|improve this answer






























                  2














                  Use grep -o:



                  grep -o "[^ ]*master[^ ]*" <<<"$list"


                  If you know that you always have just master* and worker*, you can use Shell methods:



                  echo "${list// *worker[^ ]*/}"





                  share|improve this answer




























                    2












                    2








                    2







                    Use grep -o:



                    grep -o "[^ ]*master[^ ]*" <<<"$list"


                    If you know that you always have just master* and worker*, you can use Shell methods:



                    echo "${list// *worker[^ ]*/}"





                    share|improve this answer















                    Use grep -o:



                    grep -o "[^ ]*master[^ ]*" <<<"$list"


                    If you know that you always have just master* and worker*, you can use Shell methods:



                    echo "${list// *worker[^ ]*/}"






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 7 hours ago

























                    answered 7 hours ago









                    RoVoRoVo

                    3,161216




                    3,161216























                        0














                        grep -ow 'master[^ ]*' <<<"$list"


                        or, with GNU grep,



                        grep -Pow 'masterS+' <<<"$list"


                        The -o would extract the matching bit of the string in $list, and -w would ensure that we don't match themaster or some other word that does not start with master.



                        The S in the second command is a PCRE that will match any non-space character.






                        share|improve this answer




























                          0














                          grep -ow 'master[^ ]*' <<<"$list"


                          or, with GNU grep,



                          grep -Pow 'masterS+' <<<"$list"


                          The -o would extract the matching bit of the string in $list, and -w would ensure that we don't match themaster or some other word that does not start with master.



                          The S in the second command is a PCRE that will match any non-space character.






                          share|improve this answer


























                            0












                            0








                            0







                            grep -ow 'master[^ ]*' <<<"$list"


                            or, with GNU grep,



                            grep -Pow 'masterS+' <<<"$list"


                            The -o would extract the matching bit of the string in $list, and -w would ensure that we don't match themaster or some other word that does not start with master.



                            The S in the second command is a PCRE that will match any non-space character.






                            share|improve this answer













                            grep -ow 'master[^ ]*' <<<"$list"


                            or, with GNU grep,



                            grep -Pow 'masterS+' <<<"$list"


                            The -o would extract the matching bit of the string in $list, and -w would ensure that we don't match themaster or some other word that does not start with master.



                            The S in the second command is a PCRE that will match any non-space character.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 49 mins ago









                            KusalanandaKusalananda

                            131k17249408




                            131k17249408






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501609%2fbash-find-specific-word-in-space-delimited-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

                                Histoire des bourses de valeurs

                                Why is there Russian traffic in my log files?

                                Rename multiple files to decrement number in file name?