Take output from Grep and select parts for Variables

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











up vote
1
down vote

favorite












Ok, there are similar questions, but not exactly what I am looking for.



I am working with Kubernetes, so the information is from that, but this question is for Shell Scripting, so please don't push me off to Kubernetes people. :)



I run the following command to get the information:



kubectl get pods -o wide --all-namespaces | grep sonarqube-


Result Example:



default sonarqube-664b4fd48-g6nvb 1/1 Running 0 4d 10.233.64.6 local-node-0


Goal: What I want is to take the first 2 values (default and sonarqube-664b4fd48-g6nvb) and turn them into variables that I can use elsewhere in the script.



Command Sample I want to use the variables in is like:



kubectl cp <file> $namespace/$deployment:/opt/app/extensions/plugins/


Thanks, and please let me know if more information is needed.







share|improve this question


























    up vote
    1
    down vote

    favorite












    Ok, there are similar questions, but not exactly what I am looking for.



    I am working with Kubernetes, so the information is from that, but this question is for Shell Scripting, so please don't push me off to Kubernetes people. :)



    I run the following command to get the information:



    kubectl get pods -o wide --all-namespaces | grep sonarqube-


    Result Example:



    default sonarqube-664b4fd48-g6nvb 1/1 Running 0 4d 10.233.64.6 local-node-0


    Goal: What I want is to take the first 2 values (default and sonarqube-664b4fd48-g6nvb) and turn them into variables that I can use elsewhere in the script.



    Command Sample I want to use the variables in is like:



    kubectl cp <file> $namespace/$deployment:/opt/app/extensions/plugins/


    Thanks, and please let me know if more information is needed.







    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Ok, there are similar questions, but not exactly what I am looking for.



      I am working with Kubernetes, so the information is from that, but this question is for Shell Scripting, so please don't push me off to Kubernetes people. :)



      I run the following command to get the information:



      kubectl get pods -o wide --all-namespaces | grep sonarqube-


      Result Example:



      default sonarqube-664b4fd48-g6nvb 1/1 Running 0 4d 10.233.64.6 local-node-0


      Goal: What I want is to take the first 2 values (default and sonarqube-664b4fd48-g6nvb) and turn them into variables that I can use elsewhere in the script.



      Command Sample I want to use the variables in is like:



      kubectl cp <file> $namespace/$deployment:/opt/app/extensions/plugins/


      Thanks, and please let me know if more information is needed.







      share|improve this question














      Ok, there are similar questions, but not exactly what I am looking for.



      I am working with Kubernetes, so the information is from that, but this question is for Shell Scripting, so please don't push me off to Kubernetes people. :)



      I run the following command to get the information:



      kubectl get pods -o wide --all-namespaces | grep sonarqube-


      Result Example:



      default sonarqube-664b4fd48-g6nvb 1/1 Running 0 4d 10.233.64.6 local-node-0


      Goal: What I want is to take the first 2 values (default and sonarqube-664b4fd48-g6nvb) and turn them into variables that I can use elsewhere in the script.



      Command Sample I want to use the variables in is like:



      kubectl cp <file> $namespace/$deployment:/opt/app/extensions/plugins/


      Thanks, and please let me know if more information is needed.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 6 at 21:54









      ilkkachu

      49.2k672136




      49.2k672136










      asked Mar 6 at 21:35









      Dennis Christilaw

      84




      84




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          generally



          What will work in bash and in other shells is to set positional parameters from the output of the pipe:



          set -f -- $(kubectl get pods -o wide --all-namespaces | grep sonarqube- )


          In this case, you can now access the variables as $1, $2, etc.



          specifc for bash



          In bash it is possible to create an array variable, like so:



          var=( $(kubectl get pods -o wide --all-namespaces | grep sonarqube- ) )


          The idiom is var=( ... ), in this case the output of your grep pipe.



          You can then access elements of your array using the form $var[n], with n starting at zero.






          share|improve this answer






















          • This worked perfectly! Thanks! (tried to up-vote, but need more street cred I guess).
            – Dennis Christilaw
            Mar 6 at 21:46










          • @DennisChristilaw That's ok. The expected thing to do is to mark the answer accepted with the check mark.
            – user1404316
            Mar 6 at 21:49







          • 2




            As long as the first two words in the output don't contain wildcards... Something like * as the first output word would fill the positional parameters with the filenames in the current directory, which is not what you want. Use set -f first if that's a risk.
            – ilkkachu
            Mar 6 at 21:57










          • @ilkkachu - Quite right, The OP is explicitly and specifically asking about parsing the output of kubectl, but your comment is proper for me to amend my answer, thanks.
            – user1404316
            Mar 6 at 22:08










          • @user1404316 Even though the output was from kubectl, this question/answer helped in other applications too. Thanks to the answer and help, I now have something I can use for more than just kubectl output. :)
            – Dennis Christilaw
            Mar 6 at 22:37

















          up vote
          3
          down vote













          With bash's read function:



          read -r namespace deployment other < <(kubectl get pods -o wide --all-namespaces | grep sonarqube-)
          kubectl cp <file> "$namespace/$deployment:/opt/app/extensions/plugins/"





          share|improve this answer




















          • Very good. This approach nicely avoids the common bash traps of unexpected word splitting and pathname expansion.
            – John1024
            Mar 6 at 21:46











          • @John1024, thanks
            – RomanPerekhrest
            Mar 6 at 21:47

















          up vote
          0
          down vote













          In cases like this (with predictable output columns), I usually reach for awk. Something like this might be useful:



          kubectl get pods -o wide --all-namespaces | awk '/sonarqube-/ cmd=sprintf("kubectl cp <file> %s/%s:/opt/app/extensions/plugins", $1, $2); print(cmd); system(cmd); ' 





          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%2f428614%2ftake-output-from-grep-and-select-parts-for-variables%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
            1
            down vote



            accepted










            generally



            What will work in bash and in other shells is to set positional parameters from the output of the pipe:



            set -f -- $(kubectl get pods -o wide --all-namespaces | grep sonarqube- )


            In this case, you can now access the variables as $1, $2, etc.



            specifc for bash



            In bash it is possible to create an array variable, like so:



            var=( $(kubectl get pods -o wide --all-namespaces | grep sonarqube- ) )


            The idiom is var=( ... ), in this case the output of your grep pipe.



            You can then access elements of your array using the form $var[n], with n starting at zero.






            share|improve this answer






















            • This worked perfectly! Thanks! (tried to up-vote, but need more street cred I guess).
              – Dennis Christilaw
              Mar 6 at 21:46










            • @DennisChristilaw That's ok. The expected thing to do is to mark the answer accepted with the check mark.
              – user1404316
              Mar 6 at 21:49







            • 2




              As long as the first two words in the output don't contain wildcards... Something like * as the first output word would fill the positional parameters with the filenames in the current directory, which is not what you want. Use set -f first if that's a risk.
              – ilkkachu
              Mar 6 at 21:57










            • @ilkkachu - Quite right, The OP is explicitly and specifically asking about parsing the output of kubectl, but your comment is proper for me to amend my answer, thanks.
              – user1404316
              Mar 6 at 22:08










            • @user1404316 Even though the output was from kubectl, this question/answer helped in other applications too. Thanks to the answer and help, I now have something I can use for more than just kubectl output. :)
              – Dennis Christilaw
              Mar 6 at 22:37














            up vote
            1
            down vote



            accepted










            generally



            What will work in bash and in other shells is to set positional parameters from the output of the pipe:



            set -f -- $(kubectl get pods -o wide --all-namespaces | grep sonarqube- )


            In this case, you can now access the variables as $1, $2, etc.



            specifc for bash



            In bash it is possible to create an array variable, like so:



            var=( $(kubectl get pods -o wide --all-namespaces | grep sonarqube- ) )


            The idiom is var=( ... ), in this case the output of your grep pipe.



            You can then access elements of your array using the form $var[n], with n starting at zero.






            share|improve this answer






















            • This worked perfectly! Thanks! (tried to up-vote, but need more street cred I guess).
              – Dennis Christilaw
              Mar 6 at 21:46










            • @DennisChristilaw That's ok. The expected thing to do is to mark the answer accepted with the check mark.
              – user1404316
              Mar 6 at 21:49







            • 2




              As long as the first two words in the output don't contain wildcards... Something like * as the first output word would fill the positional parameters with the filenames in the current directory, which is not what you want. Use set -f first if that's a risk.
              – ilkkachu
              Mar 6 at 21:57










            • @ilkkachu - Quite right, The OP is explicitly and specifically asking about parsing the output of kubectl, but your comment is proper for me to amend my answer, thanks.
              – user1404316
              Mar 6 at 22:08










            • @user1404316 Even though the output was from kubectl, this question/answer helped in other applications too. Thanks to the answer and help, I now have something I can use for more than just kubectl output. :)
              – Dennis Christilaw
              Mar 6 at 22:37












            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            generally



            What will work in bash and in other shells is to set positional parameters from the output of the pipe:



            set -f -- $(kubectl get pods -o wide --all-namespaces | grep sonarqube- )


            In this case, you can now access the variables as $1, $2, etc.



            specifc for bash



            In bash it is possible to create an array variable, like so:



            var=( $(kubectl get pods -o wide --all-namespaces | grep sonarqube- ) )


            The idiom is var=( ... ), in this case the output of your grep pipe.



            You can then access elements of your array using the form $var[n], with n starting at zero.






            share|improve this answer














            generally



            What will work in bash and in other shells is to set positional parameters from the output of the pipe:



            set -f -- $(kubectl get pods -o wide --all-namespaces | grep sonarqube- )


            In this case, you can now access the variables as $1, $2, etc.



            specifc for bash



            In bash it is possible to create an array variable, like so:



            var=( $(kubectl get pods -o wide --all-namespaces | grep sonarqube- ) )


            The idiom is var=( ... ), in this case the output of your grep pipe.



            You can then access elements of your array using the form $var[n], with n starting at zero.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 6 at 22:08

























            answered Mar 6 at 21:41









            user1404316

            2,314520




            2,314520











            • This worked perfectly! Thanks! (tried to up-vote, but need more street cred I guess).
              – Dennis Christilaw
              Mar 6 at 21:46










            • @DennisChristilaw That's ok. The expected thing to do is to mark the answer accepted with the check mark.
              – user1404316
              Mar 6 at 21:49







            • 2




              As long as the first two words in the output don't contain wildcards... Something like * as the first output word would fill the positional parameters with the filenames in the current directory, which is not what you want. Use set -f first if that's a risk.
              – ilkkachu
              Mar 6 at 21:57










            • @ilkkachu - Quite right, The OP is explicitly and specifically asking about parsing the output of kubectl, but your comment is proper for me to amend my answer, thanks.
              – user1404316
              Mar 6 at 22:08










            • @user1404316 Even though the output was from kubectl, this question/answer helped in other applications too. Thanks to the answer and help, I now have something I can use for more than just kubectl output. :)
              – Dennis Christilaw
              Mar 6 at 22:37
















            • This worked perfectly! Thanks! (tried to up-vote, but need more street cred I guess).
              – Dennis Christilaw
              Mar 6 at 21:46










            • @DennisChristilaw That's ok. The expected thing to do is to mark the answer accepted with the check mark.
              – user1404316
              Mar 6 at 21:49







            • 2




              As long as the first two words in the output don't contain wildcards... Something like * as the first output word would fill the positional parameters with the filenames in the current directory, which is not what you want. Use set -f first if that's a risk.
              – ilkkachu
              Mar 6 at 21:57










            • @ilkkachu - Quite right, The OP is explicitly and specifically asking about parsing the output of kubectl, but your comment is proper for me to amend my answer, thanks.
              – user1404316
              Mar 6 at 22:08










            • @user1404316 Even though the output was from kubectl, this question/answer helped in other applications too. Thanks to the answer and help, I now have something I can use for more than just kubectl output. :)
              – Dennis Christilaw
              Mar 6 at 22:37















            This worked perfectly! Thanks! (tried to up-vote, but need more street cred I guess).
            – Dennis Christilaw
            Mar 6 at 21:46




            This worked perfectly! Thanks! (tried to up-vote, but need more street cred I guess).
            – Dennis Christilaw
            Mar 6 at 21:46












            @DennisChristilaw That's ok. The expected thing to do is to mark the answer accepted with the check mark.
            – user1404316
            Mar 6 at 21:49





            @DennisChristilaw That's ok. The expected thing to do is to mark the answer accepted with the check mark.
            – user1404316
            Mar 6 at 21:49





            2




            2




            As long as the first two words in the output don't contain wildcards... Something like * as the first output word would fill the positional parameters with the filenames in the current directory, which is not what you want. Use set -f first if that's a risk.
            – ilkkachu
            Mar 6 at 21:57




            As long as the first two words in the output don't contain wildcards... Something like * as the first output word would fill the positional parameters with the filenames in the current directory, which is not what you want. Use set -f first if that's a risk.
            – ilkkachu
            Mar 6 at 21:57












            @ilkkachu - Quite right, The OP is explicitly and specifically asking about parsing the output of kubectl, but your comment is proper for me to amend my answer, thanks.
            – user1404316
            Mar 6 at 22:08




            @ilkkachu - Quite right, The OP is explicitly and specifically asking about parsing the output of kubectl, but your comment is proper for me to amend my answer, thanks.
            – user1404316
            Mar 6 at 22:08












            @user1404316 Even though the output was from kubectl, this question/answer helped in other applications too. Thanks to the answer and help, I now have something I can use for more than just kubectl output. :)
            – Dennis Christilaw
            Mar 6 at 22:37




            @user1404316 Even though the output was from kubectl, this question/answer helped in other applications too. Thanks to the answer and help, I now have something I can use for more than just kubectl output. :)
            – Dennis Christilaw
            Mar 6 at 22:37












            up vote
            3
            down vote













            With bash's read function:



            read -r namespace deployment other < <(kubectl get pods -o wide --all-namespaces | grep sonarqube-)
            kubectl cp <file> "$namespace/$deployment:/opt/app/extensions/plugins/"





            share|improve this answer




















            • Very good. This approach nicely avoids the common bash traps of unexpected word splitting and pathname expansion.
              – John1024
              Mar 6 at 21:46











            • @John1024, thanks
              – RomanPerekhrest
              Mar 6 at 21:47














            up vote
            3
            down vote













            With bash's read function:



            read -r namespace deployment other < <(kubectl get pods -o wide --all-namespaces | grep sonarqube-)
            kubectl cp <file> "$namespace/$deployment:/opt/app/extensions/plugins/"





            share|improve this answer




















            • Very good. This approach nicely avoids the common bash traps of unexpected word splitting and pathname expansion.
              – John1024
              Mar 6 at 21:46











            • @John1024, thanks
              – RomanPerekhrest
              Mar 6 at 21:47












            up vote
            3
            down vote










            up vote
            3
            down vote









            With bash's read function:



            read -r namespace deployment other < <(kubectl get pods -o wide --all-namespaces | grep sonarqube-)
            kubectl cp <file> "$namespace/$deployment:/opt/app/extensions/plugins/"





            share|improve this answer












            With bash's read function:



            read -r namespace deployment other < <(kubectl get pods -o wide --all-namespaces | grep sonarqube-)
            kubectl cp <file> "$namespace/$deployment:/opt/app/extensions/plugins/"






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 6 at 21:43









            RomanPerekhrest

            22.4k12144




            22.4k12144











            • Very good. This approach nicely avoids the common bash traps of unexpected word splitting and pathname expansion.
              – John1024
              Mar 6 at 21:46











            • @John1024, thanks
              – RomanPerekhrest
              Mar 6 at 21:47
















            • Very good. This approach nicely avoids the common bash traps of unexpected word splitting and pathname expansion.
              – John1024
              Mar 6 at 21:46











            • @John1024, thanks
              – RomanPerekhrest
              Mar 6 at 21:47















            Very good. This approach nicely avoids the common bash traps of unexpected word splitting and pathname expansion.
            – John1024
            Mar 6 at 21:46





            Very good. This approach nicely avoids the common bash traps of unexpected word splitting and pathname expansion.
            – John1024
            Mar 6 at 21:46













            @John1024, thanks
            – RomanPerekhrest
            Mar 6 at 21:47




            @John1024, thanks
            – RomanPerekhrest
            Mar 6 at 21:47










            up vote
            0
            down vote













            In cases like this (with predictable output columns), I usually reach for awk. Something like this might be useful:



            kubectl get pods -o wide --all-namespaces | awk '/sonarqube-/ cmd=sprintf("kubectl cp <file> %s/%s:/opt/app/extensions/plugins", $1, $2); print(cmd); system(cmd); ' 





            share|improve this answer
























              up vote
              0
              down vote













              In cases like this (with predictable output columns), I usually reach for awk. Something like this might be useful:



              kubectl get pods -o wide --all-namespaces | awk '/sonarqube-/ cmd=sprintf("kubectl cp <file> %s/%s:/opt/app/extensions/plugins", $1, $2); print(cmd); system(cmd); ' 





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                In cases like this (with predictable output columns), I usually reach for awk. Something like this might be useful:



                kubectl get pods -o wide --all-namespaces | awk '/sonarqube-/ cmd=sprintf("kubectl cp <file> %s/%s:/opt/app/extensions/plugins", $1, $2); print(cmd); system(cmd); ' 





                share|improve this answer












                In cases like this (with predictable output columns), I usually reach for awk. Something like this might be useful:



                kubectl get pods -o wide --all-namespaces | awk '/sonarqube-/ cmd=sprintf("kubectl cp <file> %s/%s:/opt/app/extensions/plugins", $1, $2); print(cmd); system(cmd); ' 






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 6 at 21:54









                Phys Brain

                1112




                1112






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428614%2ftake-output-from-grep-and-select-parts-for-variables%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