Select second field in text table with shell script












1












$begingroup$


I am writing a bash script to mount openshift service accounts into kubernetes objects. To find the right secret to use I need to select is highlighted in the text:




$ oc describe sa sa-build-webhook-realworld
Name: sa-build-webhook-realworld
Namespace: your-eng2
Labels: app=sa-build-webhook-realworld
Annotations: <none>
Image pull secrets: sa-build-webhook-realworld-dockercfg-4qz9g
Mountable secrets: sa-build-webhook-realworld-token-bqtnw
sa-build-webhook-realworld-dockercfg-4qz9g
Tokens: sa-build-webhook-realworld-token-bqtnw
sa-build-webhook-realworld-token-k7lq8
Events: <none>


I want the code to be reasonably robust so I am thinking of this as a job for awk where I need "anything in column 2 that is a 'Mountable secret' that isn't the docker secret". Here is the logic I have come up with:



MOUNTABLE_SECRETS='Mountable secrets:'
SECRET_NAME=$(
oc describe sa sa-build-webhook-realworld
| sed $(printf 's/./&,/%s' ${#MOUNTABLE_SECRETS})
| awk 'BEGIN{FS=OFS=","} {if ($1 ~ /^[ t]*$/) $1=ch; else ch=$1} 1'
| grep "$MOUNTABLE_SECRETS"
| sed 's/[, ]*//g'
| awk -F':' '{print $2}'
| grep -v docker
| grep token)
echo "SECRET_NAME=$SECRET_NAME"


Basically, I insert a character just beyond the width of that phrase to cut the table in half, copy cells on the left into blanks below, then select the second column then grep what I am looking for.



To my mind, it breaks things into pieces that can be understood. It works, but past experiences have taught me that someone looking to maintain that may not be best pleased. Since performance is not an issue what I am really aiming for is maintainability. I also want portability and to not be using something like Python that folks might not happen to have on their Mac. So am looking to stick to typical bash coreutils like tools.



How might I improve that script?










share|improve this question











$endgroup$












  • $begingroup$
    macOS comes with Python 2 pre-installed.
    $endgroup$
    – 200_success
    1 hour ago










  • $begingroup$
    fair enough but i am disinterested in worrying about the three major OSes and major linux distros. clearly someone submitting answers in whatever they think is a good fit works for me.
    $endgroup$
    – simbo1905
    1 hour ago


















1












$begingroup$


I am writing a bash script to mount openshift service accounts into kubernetes objects. To find the right secret to use I need to select is highlighted in the text:




$ oc describe sa sa-build-webhook-realworld
Name: sa-build-webhook-realworld
Namespace: your-eng2
Labels: app=sa-build-webhook-realworld
Annotations: <none>
Image pull secrets: sa-build-webhook-realworld-dockercfg-4qz9g
Mountable secrets: sa-build-webhook-realworld-token-bqtnw
sa-build-webhook-realworld-dockercfg-4qz9g
Tokens: sa-build-webhook-realworld-token-bqtnw
sa-build-webhook-realworld-token-k7lq8
Events: <none>


I want the code to be reasonably robust so I am thinking of this as a job for awk where I need "anything in column 2 that is a 'Mountable secret' that isn't the docker secret". Here is the logic I have come up with:



MOUNTABLE_SECRETS='Mountable secrets:'
SECRET_NAME=$(
oc describe sa sa-build-webhook-realworld
| sed $(printf 's/./&,/%s' ${#MOUNTABLE_SECRETS})
| awk 'BEGIN{FS=OFS=","} {if ($1 ~ /^[ t]*$/) $1=ch; else ch=$1} 1'
| grep "$MOUNTABLE_SECRETS"
| sed 's/[, ]*//g'
| awk -F':' '{print $2}'
| grep -v docker
| grep token)
echo "SECRET_NAME=$SECRET_NAME"


Basically, I insert a character just beyond the width of that phrase to cut the table in half, copy cells on the left into blanks below, then select the second column then grep what I am looking for.



To my mind, it breaks things into pieces that can be understood. It works, but past experiences have taught me that someone looking to maintain that may not be best pleased. Since performance is not an issue what I am really aiming for is maintainability. I also want portability and to not be using something like Python that folks might not happen to have on their Mac. So am looking to stick to typical bash coreutils like tools.



How might I improve that script?










share|improve this question











$endgroup$












  • $begingroup$
    macOS comes with Python 2 pre-installed.
    $endgroup$
    – 200_success
    1 hour ago










  • $begingroup$
    fair enough but i am disinterested in worrying about the three major OSes and major linux distros. clearly someone submitting answers in whatever they think is a good fit works for me.
    $endgroup$
    – simbo1905
    1 hour ago
















1












1








1





$begingroup$


I am writing a bash script to mount openshift service accounts into kubernetes objects. To find the right secret to use I need to select is highlighted in the text:




$ oc describe sa sa-build-webhook-realworld
Name: sa-build-webhook-realworld
Namespace: your-eng2
Labels: app=sa-build-webhook-realworld
Annotations: <none>
Image pull secrets: sa-build-webhook-realworld-dockercfg-4qz9g
Mountable secrets: sa-build-webhook-realworld-token-bqtnw
sa-build-webhook-realworld-dockercfg-4qz9g
Tokens: sa-build-webhook-realworld-token-bqtnw
sa-build-webhook-realworld-token-k7lq8
Events: <none>


I want the code to be reasonably robust so I am thinking of this as a job for awk where I need "anything in column 2 that is a 'Mountable secret' that isn't the docker secret". Here is the logic I have come up with:



MOUNTABLE_SECRETS='Mountable secrets:'
SECRET_NAME=$(
oc describe sa sa-build-webhook-realworld
| sed $(printf 's/./&,/%s' ${#MOUNTABLE_SECRETS})
| awk 'BEGIN{FS=OFS=","} {if ($1 ~ /^[ t]*$/) $1=ch; else ch=$1} 1'
| grep "$MOUNTABLE_SECRETS"
| sed 's/[, ]*//g'
| awk -F':' '{print $2}'
| grep -v docker
| grep token)
echo "SECRET_NAME=$SECRET_NAME"


Basically, I insert a character just beyond the width of that phrase to cut the table in half, copy cells on the left into blanks below, then select the second column then grep what I am looking for.



To my mind, it breaks things into pieces that can be understood. It works, but past experiences have taught me that someone looking to maintain that may not be best pleased. Since performance is not an issue what I am really aiming for is maintainability. I also want portability and to not be using something like Python that folks might not happen to have on their Mac. So am looking to stick to typical bash coreutils like tools.



How might I improve that script?










share|improve this question











$endgroup$




I am writing a bash script to mount openshift service accounts into kubernetes objects. To find the right secret to use I need to select is highlighted in the text:




$ oc describe sa sa-build-webhook-realworld
Name: sa-build-webhook-realworld
Namespace: your-eng2
Labels: app=sa-build-webhook-realworld
Annotations: <none>
Image pull secrets: sa-build-webhook-realworld-dockercfg-4qz9g
Mountable secrets: sa-build-webhook-realworld-token-bqtnw
sa-build-webhook-realworld-dockercfg-4qz9g
Tokens: sa-build-webhook-realworld-token-bqtnw
sa-build-webhook-realworld-token-k7lq8
Events: <none>


I want the code to be reasonably robust so I am thinking of this as a job for awk where I need "anything in column 2 that is a 'Mountable secret' that isn't the docker secret". Here is the logic I have come up with:



MOUNTABLE_SECRETS='Mountable secrets:'
SECRET_NAME=$(
oc describe sa sa-build-webhook-realworld
| sed $(printf 's/./&,/%s' ${#MOUNTABLE_SECRETS})
| awk 'BEGIN{FS=OFS=","} {if ($1 ~ /^[ t]*$/) $1=ch; else ch=$1} 1'
| grep "$MOUNTABLE_SECRETS"
| sed 's/[, ]*//g'
| awk -F':' '{print $2}'
| grep -v docker
| grep token)
echo "SECRET_NAME=$SECRET_NAME"


Basically, I insert a character just beyond the width of that phrase to cut the table in half, copy cells on the left into blanks below, then select the second column then grep what I am looking for.



To my mind, it breaks things into pieces that can be understood. It works, but past experiences have taught me that someone looking to maintain that may not be best pleased. Since performance is not an issue what I am really aiming for is maintainability. I also want portability and to not be using something like Python that folks might not happen to have on their Mac. So am looking to stick to typical bash coreutils like tools.



How might I improve that script?







bash linux awk sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 49 mins ago









200_success

129k15153415




129k15153415










asked 1 hour ago









simbo1905simbo1905

27127




27127












  • $begingroup$
    macOS comes with Python 2 pre-installed.
    $endgroup$
    – 200_success
    1 hour ago










  • $begingroup$
    fair enough but i am disinterested in worrying about the three major OSes and major linux distros. clearly someone submitting answers in whatever they think is a good fit works for me.
    $endgroup$
    – simbo1905
    1 hour ago




















  • $begingroup$
    macOS comes with Python 2 pre-installed.
    $endgroup$
    – 200_success
    1 hour ago










  • $begingroup$
    fair enough but i am disinterested in worrying about the three major OSes and major linux distros. clearly someone submitting answers in whatever they think is a good fit works for me.
    $endgroup$
    – simbo1905
    1 hour ago


















$begingroup$
macOS comes with Python 2 pre-installed.
$endgroup$
– 200_success
1 hour ago




$begingroup$
macOS comes with Python 2 pre-installed.
$endgroup$
– 200_success
1 hour ago












$begingroup$
fair enough but i am disinterested in worrying about the three major OSes and major linux distros. clearly someone submitting answers in whatever they think is a good fit works for me.
$endgroup$
– simbo1905
1 hour ago






$begingroup$
fair enough but i am disinterested in worrying about the three major OSes and major linux distros. clearly someone submitting answers in whatever they think is a good fit works for me.
$endgroup$
– simbo1905
1 hour ago












1 Answer
1






active

oldest

votes


















2












$begingroup$

Your idea to transform the table so that the implied keys explicitly appear on every row is an interesting one, but I think that it is overcomplicated.



The backslashes to indicate continuation lines are actually superfluous here, since an unfinished $( substitution automatically causes the command to be incomplete. Similarly, ending a line with a | pipe would also cause the command to be continued, so that would be a better convention to follow than putting the | at the beginning of the following line.



In general, any combination of sed, awk, and grep would be better expressed using just an AWK script. The AWK script below reads a key and value if there is a colon on a line, or just a value if there is no colon.



SECRET_NAME=$(
oc describe sa sa-build-webhook-realworld |
awk -F: '
$2 { KEY=$1 ; VALUE=$2; sub("^ *", "", VALUE); }
!$2 { VALUE=$1; sub("^ *", "", VALUE); }
KEY=="Mountable secrets" && VALUE !~ /docker/ { print VALUE }
'
)





share|improve this answer











$endgroup$









  • 1




    $begingroup$
    it’s a work of art!
    $endgroup$
    – simbo1905
    1 hour ago











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

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: "196"
};
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%2fcodereview.stackexchange.com%2fquestions%2f212092%2fselect-second-field-in-text-table-with-shell-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2












$begingroup$

Your idea to transform the table so that the implied keys explicitly appear on every row is an interesting one, but I think that it is overcomplicated.



The backslashes to indicate continuation lines are actually superfluous here, since an unfinished $( substitution automatically causes the command to be incomplete. Similarly, ending a line with a | pipe would also cause the command to be continued, so that would be a better convention to follow than putting the | at the beginning of the following line.



In general, any combination of sed, awk, and grep would be better expressed using just an AWK script. The AWK script below reads a key and value if there is a colon on a line, or just a value if there is no colon.



SECRET_NAME=$(
oc describe sa sa-build-webhook-realworld |
awk -F: '
$2 { KEY=$1 ; VALUE=$2; sub("^ *", "", VALUE); }
!$2 { VALUE=$1; sub("^ *", "", VALUE); }
KEY=="Mountable secrets" && VALUE !~ /docker/ { print VALUE }
'
)





share|improve this answer











$endgroup$









  • 1




    $begingroup$
    it’s a work of art!
    $endgroup$
    – simbo1905
    1 hour ago
















2












$begingroup$

Your idea to transform the table so that the implied keys explicitly appear on every row is an interesting one, but I think that it is overcomplicated.



The backslashes to indicate continuation lines are actually superfluous here, since an unfinished $( substitution automatically causes the command to be incomplete. Similarly, ending a line with a | pipe would also cause the command to be continued, so that would be a better convention to follow than putting the | at the beginning of the following line.



In general, any combination of sed, awk, and grep would be better expressed using just an AWK script. The AWK script below reads a key and value if there is a colon on a line, or just a value if there is no colon.



SECRET_NAME=$(
oc describe sa sa-build-webhook-realworld |
awk -F: '
$2 { KEY=$1 ; VALUE=$2; sub("^ *", "", VALUE); }
!$2 { VALUE=$1; sub("^ *", "", VALUE); }
KEY=="Mountable secrets" && VALUE !~ /docker/ { print VALUE }
'
)





share|improve this answer











$endgroup$









  • 1




    $begingroup$
    it’s a work of art!
    $endgroup$
    – simbo1905
    1 hour ago














2












2








2





$begingroup$

Your idea to transform the table so that the implied keys explicitly appear on every row is an interesting one, but I think that it is overcomplicated.



The backslashes to indicate continuation lines are actually superfluous here, since an unfinished $( substitution automatically causes the command to be incomplete. Similarly, ending a line with a | pipe would also cause the command to be continued, so that would be a better convention to follow than putting the | at the beginning of the following line.



In general, any combination of sed, awk, and grep would be better expressed using just an AWK script. The AWK script below reads a key and value if there is a colon on a line, or just a value if there is no colon.



SECRET_NAME=$(
oc describe sa sa-build-webhook-realworld |
awk -F: '
$2 { KEY=$1 ; VALUE=$2; sub("^ *", "", VALUE); }
!$2 { VALUE=$1; sub("^ *", "", VALUE); }
KEY=="Mountable secrets" && VALUE !~ /docker/ { print VALUE }
'
)





share|improve this answer











$endgroup$



Your idea to transform the table so that the implied keys explicitly appear on every row is an interesting one, but I think that it is overcomplicated.



The backslashes to indicate continuation lines are actually superfluous here, since an unfinished $( substitution automatically causes the command to be incomplete. Similarly, ending a line with a | pipe would also cause the command to be continued, so that would be a better convention to follow than putting the | at the beginning of the following line.



In general, any combination of sed, awk, and grep would be better expressed using just an AWK script. The AWK script below reads a key and value if there is a colon on a line, or just a value if there is no colon.



SECRET_NAME=$(
oc describe sa sa-build-webhook-realworld |
awk -F: '
$2 { KEY=$1 ; VALUE=$2; sub("^ *", "", VALUE); }
!$2 { VALUE=$1; sub("^ *", "", VALUE); }
KEY=="Mountable secrets" && VALUE !~ /docker/ { print VALUE }
'
)






share|improve this answer














share|improve this answer



share|improve this answer








edited 52 mins ago

























answered 1 hour ago









200_success200_success

129k15153415




129k15153415








  • 1




    $begingroup$
    it’s a work of art!
    $endgroup$
    – simbo1905
    1 hour ago














  • 1




    $begingroup$
    it’s a work of art!
    $endgroup$
    – simbo1905
    1 hour ago








1




1




$begingroup$
it’s a work of art!
$endgroup$
– simbo1905
1 hour ago




$begingroup$
it’s a work of art!
$endgroup$
– simbo1905
1 hour ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review 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.


Use MathJax to format equations. MathJax reference.


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%2fcodereview.stackexchange.com%2fquestions%2f212092%2fselect-second-field-in-text-table-with-shell-script%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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'