Take output from Grep and select parts for Variables
Clash 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.
bash shell-script
add a comment |Â
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.
bash shell-script
add a comment |Â
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.
bash shell-script
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.
bash shell-script
edited Mar 6 at 21:54
ilkkachu
49.2k672136
49.2k672136
asked Mar 6 at 21:35
Dennis Christilaw
84
84
add a comment |Â
add a comment |Â
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.
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. Useset -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 ofkubectl
, 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
add a comment |Â
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/"
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
add a comment |Â
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); '
add a comment |Â
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.
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. Useset -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 ofkubectl
, 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
add a comment |Â
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.
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. Useset -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 ofkubectl
, 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
add a comment |Â
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.
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.
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. Useset -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 ofkubectl
, 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
add a comment |Â
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. Useset -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 ofkubectl
, 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
add a comment |Â
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/"
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
add a comment |Â
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/"
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
add a comment |Â
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/"
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/"
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
add a comment |Â
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
add a comment |Â
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); '
add a comment |Â
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); '
add a comment |Â
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); '
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); '
answered Mar 6 at 21:54
Phys Brain
1112
1112
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password